写之前先写个现象:
string ss="";
char c='5';
ss+=c;
cout<<ss<<endl;
ss=""+c;//""+东西初始化会出问题
cout<<ss<<endl;
ss="";
ss=ss+c;
cout<<ss<<endl;
string的初始化直接用string s=""+char;
会出问题
上述代码的三次cout结果前后两个是正确的,中间一个是错误的。
1.428s(时限3s) 差点就要上 预处理+二分了
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:uva-11404 Palindromic Subsequence
* @type: dp
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123
* @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=1000+10 ;
//const int maxV=12 ;
char s[maxn];
int len;
int dp[maxn][maxn];
string str[maxn][maxn];
string empty="";
char tmp[3];
int main()
{
while(~scanf("%s",s+1))
{
len=strlen(s+1);
for(int i=1;i<=len;i++)
{
dp[i][i]=1;
dp[i][i-1]=0;
str[i][i]=s[i];
str[i][i-1]="";
}
for(int add=1;add<len;add++)
{
for(int st=1;st+add<=len;st++)
{
int ed=st+add;
dp[st][ed]=dp[st][ed-1];
str[st][ed]=str[st][ed-1];
if(dp[st][ed-1]==1&& (str[st][ed-1]>empty+s[ed]) ) str[st][ed]=s[ed];
for(int k=st;k<ed;k++) if(s[k]==s[ed])
{
if(dp[st][ed]<2+dp[k+1][ed-1])
{
dp[st][ed]=2+dp[k+1][ed-1];
str[st][ed]=s[k]+str[k+1][ed-1]+s[ed];
}
else if(dp[st][ed]==2+dp[k+1][ed-1])
{
string tmp=s[k]+str[k+1][ed-1]+s[ed];
if(tmp<str[st][ed]) str[st][ed]=tmp;
}
break;
}
}
}
cout<<str[1][len]<<endl;
}
return 0;
}
/*
computer
*/