您的位置:首页 > 其它

HDU 5340 Three Palindromes(Manacher乱搞)

2015-08-03 08:13 323 查看
Problem Description

Can we divided a given string S into three nonempty palindromes?



Input

First 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



Output

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



Sample Input

2
abc
abaadada




Sample Output

Yes
No



分析: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;
}
/*
2
aaddaaa
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: