Describe
Small hi and small ho is a pair of good friends, born in the information society, they have a great interest in programming, they agreed to help each other, in the programming of learning along the road together.
On this day, they encountered a string of strings, so little hi to small ho raised the classic question: "Little ho, you can find each of them in these strings each of the longest palindrome string it?" "
Little Ho asked curiously, "What is the longest palindrome string?" ”
Little hi replied:"a continuous paragraph in a string is the substring of this string, and palindrome string refers to the 12421 of this from the back to read and read from the back to the same string, so the longest palindrome substring is the longest string of a palindrome is the substring ~"
Little Ho Way: "That's it!" So how do I get these strings? What should I tell you about the longest palindrome string I've calculated?
Little hi smiled and said: "This is very easy, you just need to write a program, first read an integer N(n<=30)from the standard input, representing the number of strings I gave you, and then the next is the N string I want to give you (string length <=10^6 ). and you have to tell me your answer, as long as you calculate the length of the longest palindrome string in the order I give you to output to the standard output on it! you see, this is an example. "
Tip One
For a long while, Little Ho still had no clue, so he could only turn to little hi for help.
Small hi clear the throat, slowly said: "Let me start from the simple, I give you a string, can you tell me it is not a palindrome?" "
Small Ho replied: "Of course I can!" As long as the string in turn, and then compare and the original string is not the same as the line? ”
Small hi Chase asked: "That is to say you want to create a new string?" ”
Little Ho Way: "That is of course, otherwise how to compare?" ”
Little hi laughed: "but did you notice that when you compare the original string A and the new string B, the first character of A is the last character of B, and the last character of A is the first character of B, so this compares two times is not wasted efficiency? ”
Little Ho suddenly said: "It seems to be so!" I know, I don't need to create a new string, I just need to compare the first character of a and the last one, whether the second character is the same as the second, and so on, So just compare the length of the string by half the number of times, right? ”
Little hi replied: "Yes!" So you for a string, one by one enumerate its substrings, and then determine whether this substring is a palindrome substring, if yes, update the current save the longest one, is it OK? "
Little ho Happy way: "Yes!" Is this the problem? ”
Little hi sighs: "nonono! You can take a maximum of five points . ”
Little Ho regrets, said: "Only pass Ah, then how can I get more points?" ”
Hint Two
Small hi Way: "Not urgent, wait for me slowly, have you ever thought of the solution before the problem?" ”
Little Ho asked, "What's the problem?" ”
Small hi Way: "You think, if a string of [3, 7] This paragraph is not a palindrome string,[2, 8] This paragraph may also be a palindrome string it? "
Small Ho surprised way: "It seems impossible, then I did not have a lot of calculations are wasted, there is no way to solve this problem?" I have to think about it! "Say, Little Ho mused."
Long, representing the success of the smile came to the mouth of small ho: "I know!" I enumerate the substrings in a different way to enumerate, not to enumerate its starting and ending position, but try to enumerate the central location of the substring, and then from small to large to enumerate the length of the substring, once it is not a palindrome string to continue to try the next central location , so, Seems to be able to avoid a lot of problems! ”
Little hi nodded, and said, "Yes, it does reduce the time for calculation in some cases, but a string full of A, your enumeration method does not seem much use ?" But you can also get a 80 point! ”
Little Ho nodded and said: "Yes, in the worst case, this method is no better than the previous method, but my intuition tells me there must be a more efficient way to do the calculation, let me think about it!" ”
Tip Three
Small Ho This think is three days, small hi is not going to go down, decided to enlighten small ho: "Little ho, you have not thought, in the previous calculation, calculate the length of each position as the center of the longest palindrome substring of what use?" ”
"I think if the length of the longest palindrome string centered on the first 5 characters is 5 , that tells me [3, 7] This paragraph is a palindrome," replied Little ho. , so what? ”
Little hi continues to hint, "Suppose you want to calculate the length of the longest palindrome string centered on the 6th character, do you have any known information?" ”
Small Ho side said: "Well, the first 6th and 4th characters are the same, the 7th character and the 3rd character is the same, and the 5th character itself is certainly the same as the 5th character, then if [3, 5] This paragraph is a palindrome string, then [5, 7] This paragraph is definitely a palindrome string. That is, if f[i] represents the length of the longest palindrome string centered on the I -character, we will have f[i] >= f[i–2]? ”
"No, also take into account the value of f[i–1], if f[i–1] is too small to be meaningless, it should be f (i) ≥min? {f (i-2), F (i-1)-2}. Little Ho went on to add.
"Yes, but there is another problem, if I tell you at this point f (5) = 1, but f (4) = 7, f (2) = 3? "Little Hi Chase asked."
Little Ho thought, and replied, "Theoretically, I can know F (6) >=3, but because F (5) =1 so I can only figure out F (6) >=-1 I know, I should not be through F (i–1) To assist the calculation, but by making the right boundary (J + F (j)/2) The largest J to assist the calculation, so the formula will become F (i) ≥min{f (2*j-i), F (j) -2* (I-J)} in this form ! ”
Little Hi went on and asked, "What are you going to do when you know the formula?" ”
Little Ho didn't think about it. Sidewalk: "This is simple, I just need to enumerate the center position on the basis of that method, statistics make palindrome string right boundary (J + F (j)/2) The largest J, And then calculate each i , can pass f (i) ≥min? {f (2*j-i), F (j) -2* (I-J)} this formula to know f (i) a minimum , so that even in the worst case we've mentioned, we can save a lot of unnecessary calculations.
Hint Four
Flash is a week passed, small hi or did not see the small Ho written procedures, so decided to go to ask. To the small Ho home, small hi surprised to find small ho to the computer screen, a face depressed appearance, so he walked up to ask: "Little ho, how did you ah?" ”
Small Ho A little spirit also did not answer: "That is last week of the palindrome string program ah, I wrote when I found that we were thinking of the solution can only deal with the length of a palindrome substring, the length of the even-numbered palindrome string seems to be a little bit of minor changes, But after this modification, we can not use the formula we wrote last to help each other, and for a very complicated discussion , I have been thinking that there is no very beautiful way to solve the problem. ”
Little hi surprised: "You think about this for a week?" I guess you must have gone around this alley in the classified discussion, why don't you think there's no other way? ”
Little Ho asked, "What's the solution?" ”
Small hi replied: " since the length of the palindrome string is not easy to deal with, why do we not remove these palindrome strings, only the length of an odd number of palindrome string it ?" ”
Small ho sighs: "But even the length of the palindrome string can also be the answer Ah!" ”
"Unless ..." the Little Hi interposed.
"Unless what?" "Little H asked."
"You're going to turn all the even-numbered palindrome strings into odd-numbered palindrome strings, and you can think of an even number because there is no central character, but if we were to insert a special character between any two adjacent characters on the basis of the original string, is not whether the original string length is an odd number of palindrome substring or even-length palindrome substring, in the new string has an odd length of the palindrome string corresponding to it? ”
Oh I understand, so I just need to the new string according to our previous algorithm to calculate, the longest palindrome substring of the statistics to remove those special characters, is the original string of the longest palindrome substring. "Small ho happy smile, a few days of depressed is also swept away."
"But note that the longest we want to get is the longest in the original string, not the longest in the new string, after all, the number of special characters will be different because the center character is not a special character ." "Little Hi reminds me."
"NN, so I can always get full marks?" ”
"That's right!" ”
Sample input
3abababaaaaabaaacacdas
Sample output
753
1#include <iostream>2#include <string>3#include <vector>4#include <string.h>5#include <algorithm>6 using namespace:: STD;7 8 9 classpalindromestring{Ten strings; OneVector<size_t>F; A size_t i_maxpalindromelength; - - CharCentercharacter; the Public: - stringmaxpalindromestring; -Friend ostream&operator<< (ostream& o,palindromestring& s) {o<<s.s;returno;} -Friend istream&operator>> (istream& i,palindromestring& s) {i>>s.s;returni;} + //Auto operator[] (palindromestring& p,size_t i)->decltype (P.s[i]) {return p.s[i];} -size_t Lengh () {returns.length ();} +size_t Insertcentercharacter (Charc) { A stringp; atsize_t n=0; -Centercharacter=C; - for(Auto I=s.begin (); I!=s.end (); i++) { -P.push_back (*i); - P.push_back (c); -n++; in } -P.erase (P.end ()-1); tos=p; + returnN; - } the stringAcopyofremovedcentercharacter (size_t i,size_t r) { * stringP0=s.substr (i-r/2, i+r/2+1); $ stringP1;Panax Notoginseng for(Auto P=p0.begin (); P!=p0.end (); p++) { - if(*p!=centercharacter) P1.push_back (*p); the } + returnP1; A } the + size_t maxpalindromelength () - { $ f.resize (S.length ()); $Fill (F.begin (), F.end (),0); -size_t i,j=0, maxj=0; -size_t maxf=0, T; the for(i=1; I<s.length (); i++) { - if(2*j>=i && f[j]>=2* (I-j))WuyiF[i] = min (f[2*j-i],f[j]-2* (I-j)); thesize_t r=f[i]/2+1; - while(I>=r && i+r<s.length () && s[i-r]==s[i+R]) { Wur++; - } Aboutr--; $f[i]=r*2+1; - if(i+f[i]/2> Maxj) j=i; -T= (s[i+f[i]/2]==centercharacter?f[i]:f[i]+2); - if(t>MAXF) { Amaxf=T; +I_maxpalindromelength=i; themaxpalindromestring=Acopyofremovedcentercharacter (i, f[i]); - } $ } the return(maxf-1)/2; the } the }; the - in the intMainintargcConst Char*argv[]) the { AboutIos::sync_with_stdio (false); the intN; theCin>>N; the palindromestring S; + while(n--) { -Cin>>s; theS.insertcentercharacter ('*');BayiCout<<s.maxpalindromelength () <<Endl; the //cout<<s.maxpalindromestring<<endl; the } - -}
Feelings, for the need to classify the problem, and classification is not a special case, if you can construct a unified form of the solution, sacrifice some space and time can also.
#1032: Longest palindrome string