您的位置:首页 > 产品设计 > UI/UE

【poj1458】Common Subsequence || nyoj36 (动态规划)

2011-04-20 10:55 369 查看
状态转移方程:

if(st1[i]==st2[j])

res[i+1][j+1]=res[i][j]+1;

else

res[i+1][j+1]= res[i][j+1]>res[i+1][j] ?res[i][j+1]:res[i+1][j] ;

res[i][j]表示字符串字串st1[0-i],st2[0-j]的公共子序列长度。

#include<cstdio>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int res[301][301];
int main()
{
freopen("1.txt","r",stdin);
string st1,st2;
while(cin>>st1>>st2)
{
memset(res,0,sizeof(res));
for (int i=0;i<st1.length();i++)
{
for (int j=0;j<st2.length();j++)
{
if(st1[i]==st2[j])
res[i+1][j+1]=res[i][j]+1;
else res[i+1][j+1]= res[i][j+1]>res[i+1][j] ?res[i][j+1]:res[i+1][j] ;
}
}
cout<<res[st1.length()][st2.length()]<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: