Given A string, you is 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 OUTP UT 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
Idea: The longest palindrome substring, O (n^2) algorithm. This algorithm is programmed to know some grammatical problems.
For example, when using a string, such as using the cin>> input, you will encounter a space break, and get () cannot read string.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5 using namespacestd;6 #defineMAX 10107 intDp[max][max];8 intans;9 intMainintargcChar*argv[])Ten { One CharStr[max]; A //cin>>string is also read to the end of the space get cannot read string -Gets (str);//in the Cstdio - intlen=strlen (str); theMemset (DP,0,sizeof(DP)); -ans=1; - //Initialize the DP - for(intI=0; i<len;i++) + { -dp[i][i]=1; + if(i<len-1) A { at if(str[i]==str[i+1]) - { -dp[i][i+1]=1; -ans=2; - } - } in - } to for(intL=3; l<=len;l++) + { - for(intI=0; i+l-1<len;i++) the { * intj=i+l-1; $ if(str[i]==Str[j])Panax Notoginseng { -dp[i][j]=dp[i+1][j-1]; theAns=l;//How to dp[i+1][j-1]=0 the wrong answer when it's not true + } A Else thedp[i][j]=0; + } - } $cout<<ans<<Endl; $ return 0; -}
View Code
PAT1040. Longest symmetric String