Hdu 5340 the longest palindrome strand deformation

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5340

Problem Descriptioncan We divided a given string S into three nonempty palindromes?

Inputfirst line contains a single integer T≤ 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 Input
2abcabaadada

Sample Output
YesNo
/**hdu 5340 longest palindrome strand deformation topic: Given a string question whether it can be divided into three palindrome (not empty string) thinking: If the direct enumeration judgment Time complexity O (n^3). The Manacher algorithm can be used to find all 0~i for palindrome string and j~n-1 as palindrome string I and J last enumeration of the two positions using the original r[i], the middle is not a palindrome. No more complexity than O (n^2) */#include <iostream> #include <cmath> #include <string.h> #include <stdio.h># include<algorithm>using namespace Std;const int maxn=110010;/*** find the longest palindrome substring O (n) * abaaba* i:0 1 2 3 4 5 6 7 8 9 10 1 1 13* a[i]: $ # a # b # a # a # b # a #* r[i]: 1 1 2 1 4 1 2 7 2 1 4 1 2 1* answer= (max (r[i]-1)) */char a[maxn*2],    S[maxn];int r[maxn*2];int C1[20011],c2[20011];///0~i satisfies all i;j~n-1 of palindrome to satisfy palindrome all jvoid manacher (char s[],int n) {int l=0;    A[l++]= ' $ ';    a[l++]= ' # ';        for (int i=0; i<n; i++) {a[l++]=s[i];    a[l++]= ' # ';    } a[l]=0;    int mx=0,id=0;        for (int i=0; i<l; i++) {r[i]=mx>i?min (r[2*id-i],mx-i): 1;        while (A[i+r[i]]==a[i-r[i]]) r[i]++;            if (I+R[I]&GT;MX) {mx=i+r[i];        Id=i;  }}}int Main () {int T;  scanf ("%d", &t);        while (t--) {scanf ("%s", s);        int n=strn (s);        Manacher (S,n);        int l1=0,l2=0;                for (int i=1; i<2*n+2; i++) {if (r[i]==i && i!=1) {l1++;            C1[l1]=r[i]-1;                } if (R[i]==2*n+2-i && i!=2*n+1) {l2++;            C2[l2]=r[i]-1;        }} int flag=1;                for (int i=l1, i>=1&&flag; i--) {for (int j=1; j<=l2&&flag; J + +) {                int ll=c1[i]*2+2;                int rr=2*n+2-(c2[j]*2+2);                int tmp= (LL+RR)/2;                if (LL&GT;RR) continue;                if (r[tmp]-1==0) continue;                if (r[tmp]>= (rr-ll+2)/2) {flag=0;        }}} if (flag) puts ("No");    Else puts ("Yes"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 5340 the longest palindrome strand deformation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.