/*
* UVA_10100.cpp
*
* Created on: 2013年10月13日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1010;
struct text{
int num;
string word[maxn];
}t1,t2;
string s1,s2;
int f[maxn][maxn];
void devide(string s , text& t){
int l = s.size();
t.num = 1;
int i;
for(i = 0 ; i < maxn ; ++i){
t.word[i].clear();
}
for(i = 0 ; i < l ; ++i){
if('A'<= s[i] && s[i]<='Z' || 'a'<= s[i] && s[i]<='z' || '0'<= s[i] && s[i]<='9' ){
t.word[t.num] += s[i];
}else{
t.num++;
}
}
int now = 0;
for(i = 1 ; i <= t.num ; ++i){
if(!t.word[i].empty()){
t.word[++now] = t.word[i];
}
}
t.num = now;
}
int main(){
int counter = 1;
while(!cin.eof()){
memset(f,0,sizeof(f));
getline(cin,s1);
getline(cin,s2);
printf("%2d. ",counter++);
if(s1.empty() || s2.empty()){
printf("Blank!\n");
continue;
}
devide(s1,t1);
devide(s2,t2);
int i,j;
for(i = 1 ; i <= t1.num ; ++i){
for(j = 1 ; j <= t2.num ; ++j){
f[i][j] = max(f[i-1][j],f[i][j-1]);
if(t1.word[i] == t2.word[j]){
f[i][j] = max(f[i][j],f[i-1][j-1] + 1);
}
}
}
printf("Length of longest match: %d\n",f[t1.num][t2.num]);
}
return 0;
}
(DP6.1.3.1)UVA 10100 Longest Match(LCS)
最新推荐文章于 2023-02-20 20:02:10 发布