Three palindromes
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1948 Accepted Submission (s): 687
Problem Descriptioncan We divided a given string S into three nonempty palindromes?
Inputfirst line contains a single integerT≤ which denotes the number of test cases.
For each test case, the there is a single line contains a string S which only consist of lowercase Chinese letters. 1≤| S| ≤20000
Outputfor each case with the output the "Yes" or "No" in a. Sample Input2abcabaadada Sample Outputyesno
Test Instructions: give a string that asks if you can divide the string into three paragraphs, and each section is a palindrome string.
Ideas: this question needs to be used . manacher algorithm, do not know can look at this blog:42061017.
First use Manacher to find the radius of the palindrome string with each point as the midpoint, then we can know that to divide the string into three paragraphs, the first paragraph must contain the first character, the third paragraph must contain the last character. Then determine whether the first palindrome string and the remaining characters between the third form a palindrome string on the line.
So, we find all the palindrome strings found with the Manacher algorithm, which contain the first character and the last character, and then 22 pairs to see if there is a pair, they do not coincide, and the remaining characters are also palindrome strings.
See the code for specific actions:
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <string>5#include <cmath>6#include <algorithm>7#include <stack>8#include <queue>9 #defineEPS 1e-7Ten #definell Long Long One #defineINF 0x3f3f3f3f A #definePi 3.141592653589793238462643383279 - using namespacestd; - Const intMAXN =20007; the Chara[maxn],s_new[maxn<<1]; - ints_len[maxn<<1]; - - intInit ()//manacher algorithm Initialization (template) + { - intLen =strlen (a); +s_new[0] ='@'; A intj =1; at for(intI=0; i<len; ++i)//insert ' # ' around each character - { -S_new[j++] ='#'; -S_new[j++] =A[i]; - } -S_new[j++] ='#'; inS_NEW[J] =' /'; - returnJ; to } + - voidManacher (intLen//calculates the radius (template) of a palindrome centered on each point the { * intHigh =0, flag =0; $ for(intI=1; i<len; ++i)Panax Notoginseng { - if(I <High ) theS_len[i] = min (high-i+1, s_len[2*flag-i]); + Else AS_len[i] =1; the + while(S_new[i + s_len[i]] = = S_new[i-S_len[i]]) -s_len[i]++; $ $ if(i + s_len[i]-1>High ) - { -High = i+ S_len[i]-1; theFlag =i; - }Wuyi } the return; - } Wu - BOOLJudgeintLen//determine if it can be divided into 3 palindrome strings About { $ intvisit1[maxn<<1], visit2[maxn<<1], Cnt1 =0, Cnt2 =0;
//visit1 The midpoint subscript used to store all strings containing the first character - //Visit2 used to store midpoint subscripts containing the last string - - for(intI=1; i<len; ++i)//iterate over and find it in the visit array A{//because the length of the palindrome string with I as the midpoint is s_len[i]-1, it is also to determine whether the Palindrome string length is 0; + if(I-s_len[i] +1==1&& S_len[i]-1>0) thevisit1[cnt1++] =i; - if(i + s_len[i]-1= = len-1&& S_len[i]-1>0) $visit2[cnt2++] =i; the } the BOOLFlag =false; the intmid; the for(intI=0; i<cnt1; ++i)//For loop nesting for three paragraphs, the first and third 22 pairs - { in for(intj=cnt2-1; j>=0; --j) the { the if(Visit1[i] + s_len[visit1[i])-1< Visit2[j]-S_LEN[VISIT2[J]] +1)
//the selected two paragraphs can not overlap, there is a coincidence that there are no characters in the middle About { theMid = ((Visit1[i] + s_len[visit1[i])-1) + (Visit2[j]-s_len[visit2[j]] +1) ) /2;
//then take the midpoint of the remaining characters in the middle of two paragraphs the if(Mid-s_len[mid] +1<= Visit1[i] + s_len[visit1[i] && s_len[mid]-1>0)
//determines whether a palindrome with a midpoint centered completely covers all the characters in the middle. the { +Flag =true;//if covered, it means that it can be divided into three palindrome strings. - Break;//There's no need to keep looking. the }Bayi } the } the if(flag) Break; - } - returnFlag; the } the the intMain () the { - intT; theCin>>T; the while(t--) the {94scanf"%s", a); the intLen =Init (); the Manacher (len); the 98 BOOLT_or_f =judge (len); About if(t_or_f) -cout<<"yes\n";101 Else102cout<<"no\n";103 }104 return 0; the}
Hdu5340-three palindromes-(Manacher algorithm)--palindrome string