Hdu 5340 Three Palindromes maximum input string Manacher,

Source: Internet
Author: User

Hdu 5340 Three Palindromes maximum input string Manacher,
Three PalindromesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 80 Accepted Submission (s): 21


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, there is an single line contains a string S which only consist of lowercase English letters. 1 ≤ | s | ≤20000
 
OutputFor each case, output the "Yes" or "No" in a single line.
 
Sample Input

2abcabaadada
 
Sample Output
YesNo
The meaning of the question is to give a string, whether it can be divided into three non-empty return strings. We can find that the first and third strings must be a specific string of the maximum input string. Manacher can find the length of the maximum input string, enumerate the first and last strings, and directly judge in the middle, check whether the maximum input string of the midpoint is included. The complexity is o (n * n). For details about the manacher algorithm, see Manacher algorithm.
#define N 110050#define M 100005#define maxn 205#define MOD 1000000000000000007int T,n,a,pri[N],ans,len,sn = 0,top[N],tail[N],pn,ln;bool dp[N][4];pii seg[N];bool Manacher(char str[],int len){    char tstr[N+N];    int p[N + N],l2 =0,mi;    tstr[l2++] = '#';    for(int i =0;i<len;i++){        tstr[l2++] = str[i];        tstr[l2++] = '#';    }    p[0] = 0;mi = 0;    for(int i = 1;i<l2;i++){        int mi2 = mi + mi - i;        if(mi + p[mi] >= i) p[i] = min(mi2 - (mi - p[mi]),p[mi2]);        else p[i] = 0;        if(p[i] == 0 ||  mi2 - p[mi2] == mi - p[mi]){            int maxx = p[i]+1;            while(i- maxx >= 0 && i+maxx < l2 && tstr[i-maxx] == tstr[i+maxx]){                maxx++;            }            p[i] = maxx - 1;        }        if(p[i] + i > p[mi] + mi) mi = i;    }    int ans = -1;sn = 0;pn = ln = 0;    for(int i = 1;i < l2 - 1;i++){        if(i - p[i] == 0) top[pn++]  = i;        if(i + p[i] == l2 - 1) tail[ln++] = i;    }    for(int i = 0;i < pn;i++){        for(int j = ln - 1;j>=0;j--){            int s1 = top[i] + p[top[i]] + 1,s2 = tail[j] - p[tail[j]] - 1;            if(s1 > s2 )            break;            int mid = (s1 + s2)/2;            if(p[mid] >= mid - s1) return true;        }    }    return false;    //printf("%d\n",ans);}char str[N];int main(){    while(S(T)!=EOF)    {        while(T--){            SS(str);            len = strlen(str);            if(Manacher(str,len))                printf("Yes\n");            else                printf("No\n");        }    }    return 0;}


SourceBestCoder Round #49 ($)
Recommendhujie | We have carefully selected several similar problems for you: 5342 5341 5339 5338

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.