The topics are as follows:
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
The problem requires judging the longest palindrome, there are two ways to choose.
Thinking one, judging from both ends, define two pointers Start_index and End_index respectively point to the head and tail, first fixed start_index, let the end_index from the last element to traverse forward, until the encounter Start_index, In the meantime to Start_index to End_index range of palindrome judgment, palindrome judgment rule is very simple, if start and end point to the same element, palindrome length length=2, and then start+1,end-1, continue to compare, if match then continue + 2, until Start<end is no longer satisfied, notice that in this one time the element that does not conform to the start point is not equal to the element that end points to, the accumulated length is invalid and should be output 1. Note that if the form is like ABBA, start = 2, end = 1,length = 4 Meet the condition when jumping out of the loop, but if it is a form such as ABA, start=end=1,length=2 can not get the correct result, At this time should be judged whether start==end and length!=1, it is a palindrome, and need to amend, this time length+1 can get the correct results.
Note a problem, in order to be able to enter a space, use Getline (CIN,STR) to enter the string.
The code is as follows:
#include <iostream> #include <string>using namespace std;string str;int isrevese (int s, int e) { int length = 1; while (S < e) { if (str[s]! = Str[e]) return 1; s++; e--; if (length = = 1) length = 2; else length + = 2; } if (length! = 1 && s = = e) length++; return length;} int main () { getline (CIN,STR); int start_index = 0; int end_index = Str.length ()-1; int max_length =-1; int len = 0; for (start_index = 0; Start_index < Str.length (), start_index++) {for (End_index = Str.length ()-1; End_index >= Start_index; end_index--) { len = Isrevese (start_index,end_index); if (Len > Max_length) { max_length = len;}} } cout << max_length << Endl; return 0;}
Train of thought two, using the central enumeration method, with each character as the center to the end of the enumeration, this method is the biggest problem in the enumeration when the shape of the ABBA processing is very tricky, because no matter which B is enumerated, can not find the B-centric palindrome, and in fact Abba is a palindrome.
Here I see the ingenious solution of Sunbaigui, he put all the characters in front of a leading-1, so that by the 1 as the central enumeration can get the correct palindrome, for example, Abba into -1a-1b-1b-1a, through the middle of 1, the entire sequence is obtained, This time the palindrome length is twice times the correct length, only need to divide by 2 to get the correct result.
Here is the Sunbaigui code that is directly posted, welcome to his blog.
#include <iostream> #include <string.h> #include <vector> #define MAX 10000int Dp[2*max+1];char str[ Max];int Mmax (int a, int b) {if (a > B) return a;else return B;} int main () {while (gets (str)) {//memset (DP,-1, sizeof (DP)); int len = strlen (str);//insert special character into Str, must n OT appeared in strstd::vector<int> magic;for (int i = 0; i < len; ++i) {magic.push_back ( -1);//special Charactermagi C.push_back (Str[i]);//character to Int}magic.push_back ( -1),//enumerate center point for Magic Vectorlen = (int) Magic.size (); int max = 1;for (int i = 1; i < Len; ++i) {int L, r;int step = 1;for (L = i-1, r = i+1; l >= 0 && R < Len; L--, r++) {if (magic[l]! = Magic[r]) Break;step + = 2;} max = Mmax (max, step);} printf ("%d\n", MAX/2);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1040. Longest symmetric String (25)