UVa 10100 - Longest Match
// Accepted
#include<bits/stdc++.h>
#define high 10010
#define up 1
#define down -1
#define diag 0
using namespace std;
typedef vector<string>vs;
vs x,y;
string s1, s2;
int c[high][high] , b[high][high];
void LCS(int m, int n)
{
int i,j;
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1]+1;
b[i][j] = diag;
}
else if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = up;
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = down;
}
}
}
cout << "Length of longest match: " << c[m][n] << "\n";
}
void convert(bool f)
{
string tmp;
int i , len;
if(f)
{
len=s1.length();
for(i=0; i<=len; i++)
{
if((s1[i]>='A' and s1[i]<='Z') or (s1[i]>='a' and s1[i]<='z') or (s1[i]>='0' and s1[i]<='9'))
{
continue;
}
else
{
s1[i] = ' ';
}
}
stringstream ss(s1);
while(ss >> tmp)
{
x.push_back(tmp);
}
//for(i=0; i<x.size(); i++) cout << x[i] << " ";
}
else
{
len = s2.length();
for(i=0; i<=len; i++)
{
if((s2[i]>='A' and s2[i]<='Z') or (s2[i]>='a' and s2[i]<='z') or (s2[i]>='0' and s2[i]<='9'))
{
continue;
}
else
{
s2[i] = ' ';
}
}
stringstream ss(s2);
while(ss >> tmp)
{
y.push_back(tmp);
}
//for(i=0; i<y.size(); i++) cout << y[i] << " " ;
}
}
int main()
{
ios_base::sync_with_stdio(0);
int tc=0 , i;
while(getline(cin , s1))
{
getline(cin , s2);
cout << setw(2) << ++tc << ". ";
if(s1.length() == 0 or s2.length() == 0)
{
cout << "Blank!\n";
}
else
{
x.clear();
y.clear();
convert(true);
convert(false);
LCS(x.size(), y.size());
}
}
return 0;
}
#include<bits/stdc++.h>
#define high 10010
#define up 1
#define down -1
#define diag 0
using namespace std;
typedef vector<string>vs;
vs x,y;
string s1, s2;
int c[high][high] , b[high][high];
void LCS(int m, int n)
{
int i,j;
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1]+1;
b[i][j] = diag;
}
else if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = up;
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = down;
}
}
}
cout << "Length of longest match: " << c[m][n] << "\n";
}
void convert(bool f)
{
string tmp;
int i , len;
if(f)
{
len=s1.length();
for(i=0; i<=len; i++)
{
if((s1[i]>='A' and s1[i]<='Z') or (s1[i]>='a' and s1[i]<='z') or (s1[i]>='0' and s1[i]<='9'))
{
continue;
}
else
{
s1[i] = ' ';
}
}
stringstream ss(s1);
while(ss >> tmp)
{
x.push_back(tmp);
}
//for(i=0; i<x.size(); i++) cout << x[i] << " ";
}
else
{
len = s2.length();
for(i=0; i<=len; i++)
{
if((s2[i]>='A' and s2[i]<='Z') or (s2[i]>='a' and s2[i]<='z') or (s2[i]>='0' and s2[i]<='9'))
{
continue;
}
else
{
s2[i] = ' ';
}
}
stringstream ss(s2);
while(ss >> tmp)
{
y.push_back(tmp);
}
//for(i=0; i<y.size(); i++) cout << y[i] << " " ;
}
}
int main()
{
ios_base::sync_with_stdio(0);
int tc=0 , i;
while(getline(cin , s1))
{
getline(cin , s2);
cout << setw(2) << ++tc << ". ";
if(s1.length() == 0 or s2.length() == 0)
{
cout << "Blank!\n";
}
else
{
x.clear();
y.clear();
convert(true);
convert(false);
LCS(x.size(), y.size());
}
}
return 0;
}
Comments
Post a Comment