Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?
, the longest symmetric sub-string is s PAT&TAP s
, hence you must output 11
.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
我的错误代码
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #define inf 105 using namespace std; int main() { string s; int n,dp[inf][inf],maxn=0; getline(cin,s); n=s.size(); fill(dp[0],dp[0]+inf*inf,0); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(i==j) dp[i][j]=1; //if(j-i==1&&s[j]==s[i]) //dp[i][j]=1; } for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { if(s[i]==s[j]) { dp[i][j]=dp[i+1][j-1]+1; if(dp[i][j]>maxn) maxn=dp[i][j]; } else dp[i][j]=0; } cout<<maxn<<endl; return 0; }
柳神代码
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #define inf 1001 using namespace std; int main() { string s; int n,dp[inf][inf],ans=1; getline(cin,s); n=s.size(); fill(dp[0],dp[0]+inf*inf,0); for(int i=0;i<n;i++) { dp[i][i]=1; if(s[i]==s[i+1]&&i<n-1) { dp[i][i+1]=1; ans=2; } } for(int l=2;l<=n;l++) for(int i=0;l+i-1<n;i++) { int j=l+i-1; if(s[i]==s[j]&&dp[i+1][j-1]==1) { dp[i][j]=1; ans=l; } } cout<<ans<<endl; return 0; }
dp还得好好学习啊
思维不行