【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]的公共子序列长度。
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; } }
相关文章推荐
- UVA10405-Longest Common Subsequence && nyoj36-最长公共子序列
- poj1458 Common Subsequence 动态规划
- poj1458 Common Subsequence ——最长公共子序列
- NYOJ 动态规划 36题 最长公共子序列
- HDU-1159 Common Subsequence(动态规划)
- CSU-ACM2017暑期训练8-动态规划初步 C - Common Subsequence
- poj1458-Common Subsequence(最长公共子序列)
- POJ1458 Common Subsequence
- NYOJ 题目36 最长公共子序列(动态规划)
- HDU 1159 Common Subsequence(动态规划)
- [动态规划-2] 最长公共子序列-Longest Common Subsequence
- POJ 1458 Common Subsequence(最长公共子序列)__动态规划
- HDU1159 Common Subsequence(动态规划)
- UVA 10405 Longest Common Subsequence (动态规划 LCS)
- HDU1159 && POJ1458:Common Subsequence(LCS)
- NYOJ36 最长公共子序列 【动态规划】
- HDU1159 && POJ1458:Common Subsequence(LCS)
- Hdu1159 - Common Subsequence - 动态规划
- 字符串 并且 动态规划练习二:HDU ACM 1159 Common Subsequence
- HDU1159 && POJ1458:Common Subsequence(LCS)