HDU 5340 Three Palindromes(Manacher乱搞)

来源:互联网 发布:ios仿新浪微博源码 编辑:程序博客网 时间:2024/04/29 13:33
Problem Description
Can we divided a given string S into three nonempty palindromes?
 

Input
First line contains a single integer T20 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
 

Output
For each case, output the "Yes" or "No" in a single line.
 

Sample Input
2abcabaadada
 

Sample Output
YesNo
 
分析:Manacher之后乱搞。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=1e5+100;char str[maxn],ma[maxn];int mp[maxn],len,t,p[maxn];vector<int>pre;vector<int>suf;void Manacher(){    int l=0;    ma[l++]='$';ma[l++]='#';    for(int i=0;i<len;i++)    {        ma[l++]=str[i];        ma[l++]='#';    }    ma[l]='\0';    len=l-1;    int tot=0;    int mx=0,id=0,ans=0;    for(int i=0;i<l;i++)    {        mp[i]=mx>i?min(mp[2*id-i],mx-i):1;        while(ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++;        if(i+mp[i]>mx)        {            mx=i+mp[i];            id=i;        }    }}int main(){    scanf("%d",&t);    while(t--)    {        scanf("%s",str);        pre.clear();suf.clear();        len=strlen(str);        if(len<3)        {            puts("No");            continue;        }        Manacher();        for(int i=1;i<=len;i++)        {            if(mp[i]==i)                pre.push_back(i);            if(i+mp[i]-1==len)                suf.push_back(i);        }        int flag=0;        int presz=pre.size();        int sufsz=suf.size();        for(int i=0;i<presz;i++)        {            int a=pre[i];            for(int j=sufsz-1;j>=0;j--)            {                int b=suf[j];                if(a>=b) break;                int L=a+mp[a];                int R=b-mp[b];                if(L<=2||R>=len-1) continue;                if(L==R&&ma[L]=='#') continue;                if(L>R) break;                int sz=R-L+1;                int mid=(L+R)>>1;                if(mp[mid]*2-1>=sz)                    flag=1;            }            if(flag)                break;        }        puts(flag?"Yes":"No");    }    return 0;}/*2aaddaaa*/


0 0
原创粉丝点击