HDU1159 && POJ1458:Common Subsequence(LCS)
2014-12-12 22:46
447 查看
必须得啃DP了,就从最简单的DP入门题开始了。
#include <iostream> #include <cstdio> #include <string> #include <cstring> using namespace std; string str1,str2; int pos[1005][1005]; int main(){ while(cin>>str1>>str2){ int length1=str1.size(); int length2=str2.size(); memset(pos,0,sizeof(pos)); for(int i=1;i<=length1;i++){ for(int j=1;j<=length2;j++){ if(str1[i-1]==str2[j-1]){ pos[i][j]=pos[i-1][j-1]+1; } else{ pos[i][j]=max(pos[i-1][j],pos[i][j-1]); } } } cout<<pos[length1][length2]<<endl; } return 0; }滚动数组优化
#include <iostream> #include <cstdio> #include <string> #include <cstring> using namespace std; string str1,str2; int pos[2][1005]; int main(){ while(cin>>str1>>str2){ int length1=str1.size(); int length2=str2.size(); memset(pos,0,sizeof(pos)); for(int i=1;i<=length1;i++){ for(int j=1;j<=length2;j++){ if(str1[i-1]==str2[j-1]){ pos[i%2][j]=pos[(i-1)%2][j-1]+1; } else{ pos[i%2][j]=max(pos[(i-1)%2][j],pos[i%2][j-1]); } } } cout<<max(pos[0][length2],pos[1][length2])<<endl; } return 0; }
相关文章推荐
- HDU1159 && POJ1458:Common Subsequence(LCS)
- HDU1159 && POJ1458:Common Subsequence(LCS)
- hdu 1159 && poj 1458 Common Subsequence (LCS)
- POJ 1458 && HDU 1159 Common Subsequence(LCS)
- hdu 1159 && POJ 1458Common Subsequence(LCS)
- HDU1159 && POJ1458 Common Subsequence (LCS)
- HDU 1159 & POJ 1458 Common Subsequence 【LCS】
- HDU 1159 & POJ 1458 Common Subsequence(LCS 最长公共子序列O(nlogn))
- [HDU - 1159] Common Subsequence(动态规划&&LCS)
- 最长公共子序列(dp) & hdu 1159 Common Subsequence
- hdu 1159 Common Subsequence(LCS最长公共子序列)
- hdu 1159 Common Subsequence(LCS,dp)
- HDU 1159 Common Subsequence(LCS)
- hdu1159 Common Subsequence(LCS)
- hdu1159 Common Subsequence(LCS)
- Common Subsequence(hdu1159(LCS))
- hdu 1159/poj1458 Common Subsequence 最长公共子串
- POJ 1458 / HDU 1159 / Southeastern Europe 2003 Common Subsequence (DP&LCS)
- HDU 1159 Common Subsequence(LCS)
- hdu 1159 Common Subsequence (LCS)