Title Link: http://poj.org/problem?id=1458
This is a template problem of the longest common subsequence;
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <stack> #include <set> #include <vector> #include <algorithm> #define LL Long longusing namespace std;/* the longest ascending sub-sequence of the template title */int dp[1005][1005];int Main () { string s1,s2; while (CIN>>S1>>S2) { s1= ' @ ' +s1; S2= '! ' +S2; convenient post-processing, for (int i=0;i<s1.size (); i++) dp[i][s2.size ()]=0; initialized to 0; for (int i=0;i<s2.size (); i++) dp[s1.size ()][i]=0; for (int i=1;i<=s1.size (), i++) {for (int j=1;j<=s2.size (); j + +) { if (s1[i-1]==s2[j-1]) dp[i][j]=dp[ i-1][j-1]+1; if equal, then the previous plus one; else{ Dp[i][j]=max (dp[i-1][j],dp[i][j-1]); }} printf ("%d\n", Dp[s1.size ()][s2.size ()]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj-1458-common subsequence-the longest common sub-sequence of motion regulation