Three palindromes
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 809 Accepted Submission (s): 240
Problem Descriptioncan We divided a given string S into three nonempty palindromes?
Inputfirst line contains a single integer T ≤ 20 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 ≤ < Span class= "Mrow" id= "mathjax-span-11" > | s | ≤ 20000
Outputfor each case with the output the "Yes" or "No" in a.
Sample Input
2abcabaadada
Sample Output
YesNo
Sourcebestcoder Round #49 ($)
Recommendhujie
Approximate test instructions:
Determines whether a string of length 2e4 can be divided into three palindrome strings
General idea:
BC Because of the problem, got the money 2333
Manacher processing a palindrome range, then we get two arrays, A, B, respectively: character 0 position to A[pos] is a palindrome string, character len-1 position to B[pos] is a palindrome string
Then binary enumeration A[x]~b[y] is not a palindrome string, with Manachero (1) can be sentenced to come out
Manacher calculates the longest palindrome string that has been s[x], and then pushes it back to the same interval on the original string.
Previously pushed a formula, already used this formula over three questions, it is really no problem
int L = (I-mp[i]) >>1;
int R = (i+mp[i]-4) >>1;
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include < string> #include <vector> #include <cstdio> #include <ctime> #include <bitset> #include < Algorithm> #define SZ (x) ((int) (x). Size ()) #define ALL (v) (v). Begin (), (v). End () #define A foreach (I, v) for (__typeof (v ). Begin ()) I = (v). Begin (); I! = (v). End (); + + i) #define REP (i,n) for (int i=1; i<=int (n); i++) using namespace std;typedef long long ll; #define X First#define Y Secondtypedef pair<int,int> pii;template <class t>inline bool RD (T &ret) {char c; int sgn; if (c = GetChar (), c = = EOF) return 0; while (c! = '-'-' && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); SGN = (c = = '-')? -1:1; ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret= RET * + (C-' 0 '); RET *= SGN; return 1;} Template <class t>inline void PT (T x) {if (x < 0) {Putchar ('-'); x =-X; } if (x > 9) pt (X/10); Putchar (x 10 + ' 0 ');} const int N = 20000+100;int a[n],top1;int b[n],top2;char s[n];void ini () {top1 = 0; TOP2 = 0;} int Mp[n<<1];char ma[n<<1];int vs[n];void manacher (char s[],int len) {int L = 0; ma[l++] = ' $ '; ma[l++] = ' # '; for (int i=0;i<len;i++) {Ma[l] = s[i]; Vs[i] = l++; Record map to position in the original string ma[l++] = ' # '; } Ma[l] = 0; int mx = 0,id = 0; for (int i = 1;i < L; i + +) {mp[i]= mx>i? min (mp[2*id-i],mx-i): 1; while (ma[mp[i]+i] = = Ma[i-mp[i]]) mp[i]++; if (Mp[i]+i > mx) {mx = mp[i]+i; id = i; } if (Mp[i] >= 2){int L = (I-mp[i]) >>1; Inverse to the range left endpoint int R = (i+mp[i]-4) in the original string >>1; Inverse to the interval right end of the original string if (L = = 0) A[++top1] = R; if (R = = len-1) B[++top2] = L; }}}int Main () {int T; RD (T); while (t--) {scanf ("%s", s); INI (); int len = strlen (s); Manacher (S,len); Sort (A+1,A+1+TOP1); Sort (B+1,B+1+TOP2); bool OK = 0; REP (I,TOP1) {if (ok) break; for (int j = TOP2; J >= 1 && ok = = 0;j--) {int L = A[i], r = b[j]; if (r-l <= 1) break; l++,r--; if ((r-l+1) &1) {int pos = L + (r-l+1)/2; int L = (vs[Pos]-mp[vs[pos]]) >>1; int R = (vs[pos]+mp[vs[pos]]-4) >>1; if (l <= l && R <= r) OK = 1; } else {int pos = L + (r-l+1)/2; int p = vs[pos]-1; if (Mp[p] < 2) continue; int L = (P-mp[p]) >>1; int R = (p+mp[p]-4) >>1; if (l <= l && R <= r) OK = 1; }}} if (OK) puts ("Yes"); Else puts ("No"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5340 Three palindromes (binary enumeration +manacher+ record interval)