标签:des style color io os ar for strong sp
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39009 | Accepted: 15713 |
Description
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
最长公共子序列问题。
二维dp。dp[i][j]代表字符串s的前i个字符与字符串t的前j个字符的最长公共子序列的长度。dp[0][0]=0;
if(s[i]==t[j])dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);//从前状态取最大
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 510; #define LL long long int dp[maxn][maxn]={0}; char s[maxn],t[maxn]; int main() { while(scanf("%s %s",s,t)!=EOF) { int ls=strlen(s),lt=strlen(t); for(int i=1;i<=ls;i++) for(int j=1;j<=lt;j++) dp[i][j]=s[i-1]==t[j-1]?dp[i-1][j-1]+1:max(dp[i-1][j],dp[i][j-1]); printf("%d\n",dp[ls][lt]); } return 0; }
POJ 1458-Common Subsequence(线性dp/LCS)
标签:des style color io os ar for strong sp
原文地址:http://blog.csdn.net/qq_16255321/article/details/40042219