1040. Longest symmetric String (25) time limit of MS memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue
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
Submit Code
Method One: Insert invalid characters, traverse once.
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <cmath>8#include <string>9#include <map>Ten#include <Set> One using namespacestd; A intdp[20005]; - intMain () { - //freopen ("D:\\input.txt", "R", stdin); the //scanf ("%s", s); - strings; - getline (cin,s); - + //cout<<s<<endl; - + inti,j=0, k,count=0; Adp[j++]=-1; at for(i=0; I<s.length (); i++){ -Dp[j++]=s[i];//Hash -dp[j++]=-1; - } - - //cout<<j<<endl; in - for(i=1; i<j;i++) {//I start from 1!! to intf=i-1, b=i+1; + while(f>=0&&b<j&&dp[f]==Dp[b]) { -f--; theb++; * } $ if(count<b-f-1){Panax Notoginsengcount=b-f-1; - //cout<<count<<endl; the } + } Aprintf"%d\n", count/2);//here can be divided into the center for-1 and the normal number of 2 case discussion the return 0; +}
Method Two: Sub-parity discussion:
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <cmath>8#include <string>9#include <map>Ten#include <Set> One using namespacestd; A intMain () { - //freopen ("D:\\input.txt", "R", stdin); - //scanf ("%s", s); the strings; - getline (cin,s); - - //cout<<s<<endl; + - intI,j,k,count=0; + for(i=0; I<s.length (); i++){ A intf=i,b=i; at while(f>=0&&b<s.length () &&s[f]==S[b]) { -f--; -b++; - } - if(count<b-f-1){ -count=b-f-1; in } -f=i; tob=i+1; + while(f>=0&&b<s.length () &&s[f]==S[b]) { -f--; theb++; * } $ if(count<b-f-1){Panax Notoginsengcount=b-f-1; - } the } +printf"%d\n", count); A return 0; the}
pat1040. Longest symmetric String (25)