//Problem link >>http://uva.onlinejudge.org/external/101/10100.html
/// in this LCS problem u may find it hard to manage a word ... here i've used file-stream I/O ... see the header file "sstream" & the command "stringstream" ... then uor string will be separated into words ... make sure u hide uor words in the form of a character ... here "check" array do this and then apply lcs to "check" array ...
#include <iostream>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std ;
int lcs[1002][1002] ,check [1002][1002] ;
int main ()
{
string s1 , s2 , s ;
int cas=0 ;
vector<string>v1 ;
vector<string>v2 ;
while (getline(cin,s1))
{
getline(cin,s2) ;
for (int i=0 ; i<s1.size() ; i++)
{
if (s1[i]>='a'&&s1[i]<='z' || s1[i]>='A' &&s1[i]<='Z' || s1[i]>='0'&&s1[i]<='9') continue ;
else s1[i]=' ' ;
}
for (int i=0 ; i<s2.size() ; i++)
{
if (s2[i]>='a'&&s2[i]<='z' || s2[i]>='A' &&s2[i]<='Z' || s2[i]>='0'&&s2[i]<='9') continue ;
else s2[i]=' ' ;
}
v1.clear() ;
v2.clear() ;
stringstream sx(s1) ;
stringstream sy(s2) ;
while(sx>>s) v1.push_back(s) ;
while(sy>>s) v2.push_back(s) ;
int x=v1.size() ;
int y=v2.size() ;
for (int i=0 ; i<x ; i++)
{
for (int j=0 ; j<y ; j++)
{
if (v1[i]==v2[j]) check[i][j]=1 ;
else check[i][j]=0 ;
}
}
for (int i=x ; i>=0 ; i--)
{
for (int j=y ; j>=0 ; j--)
{
if (i==x||j==y)
{
lcs[i][j]=0 ;
continue ;
}
if (check[i][j]==1) lcs[i][j]=1+lcs[i+1][j+1] ;
else lcs[i][j]=max(lcs[i+1][j],lcs[i][j+1]) ;
}
}
printf("%2d.",++cas) ;
if (x==0||y==0) cout<<" Blank!"<<endl ;
else cout<<" Length of longest match: "<<lcs[0][0]<<endl ;
}
return 0 ;
}
test
Hello!
The document provides late-breaking information
late breaking.
2. Blank!
3. Length of longest match: 2
/// in this LCS problem u may find it hard to manage a word ... here i've used file-stream I/O ... see the header file "sstream" & the command "stringstream" ... then uor string will be separated into words ... make sure u hide uor words in the form of a character ... here "check" array do this and then apply lcs to "check" array ...
#include <iostream>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std ;
int lcs[1002][1002] ,check [1002][1002] ;
int main ()
{
string s1 , s2 , s ;
int cas=0 ;
vector<string>v1 ;
vector<string>v2 ;
while (getline(cin,s1))
{
getline(cin,s2) ;
for (int i=0 ; i<s1.size() ; i++)
{
if (s1[i]>='a'&&s1[i]<='z' || s1[i]>='A' &&s1[i]<='Z' || s1[i]>='0'&&s1[i]<='9') continue ;
else s1[i]=' ' ;
}
for (int i=0 ; i<s2.size() ; i++)
{
if (s2[i]>='a'&&s2[i]<='z' || s2[i]>='A' &&s2[i]<='Z' || s2[i]>='0'&&s2[i]<='9') continue ;
else s2[i]=' ' ;
}
v1.clear() ;
v2.clear() ;
stringstream sx(s1) ;
stringstream sy(s2) ;
while(sx>>s) v1.push_back(s) ;
while(sy>>s) v2.push_back(s) ;
int x=v1.size() ;
int y=v2.size() ;
for (int i=0 ; i<x ; i++)
{
for (int j=0 ; j<y ; j++)
{
if (v1[i]==v2[j]) check[i][j]=1 ;
else check[i][j]=0 ;
}
}
for (int i=x ; i>=0 ; i--)
{
for (int j=y ; j>=0 ; j--)
{
if (i==x||j==y)
{
lcs[i][j]=0 ;
continue ;
}
if (check[i][j]==1) lcs[i][j]=1+lcs[i+1][j+1] ;
else lcs[i][j]=max(lcs[i+1][j],lcs[i][j+1]) ;
}
}
printf("%2d.",++cas) ;
if (x==0||y==0) cout<<" Blank!"<<endl ;
else cout<<" Length of longest match: "<<lcs[0][0]<<endl ;
}
return 0 ;
}
Sample Input
This is a test.test
Hello!
The document provides late-breaking information
late breaking.
Sample Output
1. Length of longest match: 12. Blank!
3. Length of longest match: 2
No comments:
Post a Comment