1040. Longest shortric String (25) Longest echo substring-Marathon (manacher algorithm) -- PAT (Advanced Level) Practise
Question Information
1040. Longest shortric String (25)
Time limit 400 MS
The memory limit is 65536 kB.
Code length limit: 16000 B
Given a string, you are supposed to output the length of the longest distributed Ric sub-string. For example, given "Is PAT & TAP distributed Ric ?", The longest specified Ric 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 into Ric?
Sample Output:
11
Solutions
Manacher algorithm, do not understand can refer to my another blog: http://blog.csdn.net/xianyun2009/article/details/46767829
AC code
# Include
# Include using namespace std; char s [2010]; int r [2010]; int ch, p = 0; int manacher () {int cn = 0, mx = 0, re = 0; for (int I = 1; I <p; ++ I) {r [I] = (mx> I )? Min (r [cn * 2-I], mx-I): 1; while (s [I + r [I] = s [I-r [I]) + + r [I]; if (I + r [I]> mx) {mx = I + r [I]; cn = I;} if (r [I]> re) re = r [I];} return re-1;} int main () {s [p ++] = '\ 1 '; // special character while (ch = getchar () & ch! = '\ N') {s [p ++] =' \ 3'; s [p ++] = ch ;} s [p ++] = '\ 3'; printf ("% d \ n", manacher (); return 0 ;}