您的位置:首页 > 其它

uva 10010 Longest Match

2013-08-07 21:10 148 查看
uva  10010  Longest Match

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1041

DP LCS

题目大意:解密需求最大匹配长度

题目分析:DP解LCS问题要从LCS的三个性质入手。若A[i]==B[j],则可把此点加入作为其公共子列之一,否则,去找A[i-1]和B[j]或A[i]和B[j-1]构成的解(没解释清,这里恐怕只能去找参考资料了)。

code:

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#define N (1024)
using namespace std;
struct text
{
int num;
string word[1024];
}t1,t2;
string s1,s2;
int f

;
bool judge(char c)
{
return c>='A'&&c<='Z'||c>='a'&&c<='z'||c>='0'&&c<='9';
}
void devide(string s,text &t)
{//在这个函数里主要学学string操作
int l=s.size();
t.num=1;
for(int i=0;i<1000;i++)t.word[i].clear();
for(int i=0;i<l;i++)
{
if(judge(s[i]))t.word[t.num]+=s[i];
else t.num++;
}
int now=0;
for(int i=1;i<=t.num;i++)
{//单词缩进
if(!t.word[i].empty())t.word[++now]=t.word[i];
}
t.num=now;
}
int main()
{
int test=0;
while(!cin.eof())
{
getline(cin,s1);
getline(cin,s2);
printf("%2d. ",++test);
if(s1.empty()||s2.empty())
{
printf("Blank!\n");
continue;
}
devide(s1,t1);
devide(s2,t2);
memset(f,0,sizeof(f));
for(int i=1;i<=t1.num;i++)
{//核心思想
for(int 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]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LCS DP