标签:style blog http io color os ar for sp
这题说的是给了一个长度为n的字符串(1000)求最长回文子序列,并输出当str[i]==ste[j]时dp[i][j]=dp[i+1][i-1]+2 否则 dp[i][j]=Max(dp[j+1][i],dp[j][i-1]) 要强调一下这uva真是强大 每个后面都加一个string都不爆内存太厉害了
#include <iostream> #include <string.h> #include <algorithm> #include <string> #include <cstdio> using namespace std; const int maxn =1005; struct point{ int len; string s; bool operator <(const point &A)const{ return len<A.len||(len==A.len&&s>A.s); } }dp[maxn][maxn]; char s1[maxn],s2[maxn]; int main() { for(int i=0; i<maxn-1; ++i) dp[i][0].len=dp[0][i].len=0,dp[i][0].s=dp[0][i].s=""; while(scanf("%s",s1+1)==1){ int n= strlen(s1+1); for(int i=1;i<=n; ++i) dp[i][i].len=1,dp[i][i].s=s1[i]; for(int i=1; i<=n; ++i){ for(int j=i-1; j>=1; --j){ dp[j][i].len=0; dp[j][i].s=""; if(s1[i]==s1[j]){ dp[j][i].len=dp[j+1][i-1].len+2; dp[j][i].s+=s1[j]; dp[j][i].s+=dp[j+1][i-1].s; dp[j][i].s+=s1[i]; // if(dp[j][i]<dp[j+1][i]) dp[j][i]=dp[j+1][i]; //if(dp[j][i]<dp[j][i-1]) dp[j][i]=dp[j][i-1]; }else{ if(dp[j+1][i]<dp[j][i-1]) dp[j][i]=dp[j][i-1]; else dp[j][i]=dp[j+1][i]; } } } cout<<dp[1][n].s<<endl; } return 0; }
标签:style blog http io color os ar for sp
原文地址:http://www.cnblogs.com/Opaser/p/4054780.html