题目:http://poj.org/problem?id=1458
就是最长公共子串
f[i,j]=f[i-1,j-1]+1 相同
f[i,j]=max(f[i-1,j],f[i,j-1]) 不同
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
const int maxSize=1000;
string s1,s2;
int f[maxSize+5][maxSize+5];
void dp()
{
int i,j;
for (i=1;i<=s1.length();i++)
{
for (j=1;j<=s2.length();j++)
{
if (s1[i-1]==s2[j-1])
f[i][j]=f[i-1][j-1]+1;
else
{
f[i][j]=max(f[i-1][j],f[i][j-1]);
}
}
}
printf("%d\n",f[i-1][j-1]);
}
int main()
{
int i;
freopen("a.txt","r",stdin);
while (cin>>s1>>s2)
{
memset(f,0,sizeof(f));
// cout<<s<<endl;
dp();
}
return 0;
}