Bestcoders 回文串 Manacher 算法

Three Palindromes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 871    Accepted Submission(s): 262


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
  
  
2 abc abaadada
 

Sample Output
  
  
Yes No
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5342  5341  5339  5338  5337 
 

#include <bits/stdc++.h>

using namespace std;

int t;
int p[220100];
char s[220100], c[221000];
int len;

void palindrome()
{
    len = strlen(s);
    c[0] = '$';
    for (int i = 0; i<len; i++)
        c[i * 2 + 1] = '#', c[i * 2 + 2] = s[i];
    c[len * 2 + 1] = '#';
    len = len * 2 + 2;
    c[len] = '\0';
    int mx = 0, id = 0;
    for (int i = 1; i<len; i++)
    {
        if (mx>i) p[i] = min(p[id * 2 - i], mx - i);
        else p[i] = 1;
        while (c[i + p[i]] == c[i - p[i]]) p[i]++;
        if (i + p[i]>mx) mx = i + p[i], id = i;
    }
}

int solve()
{
    for(int i=2; i<len; i++)
    {
        if(i - p[i] != 0) continue;
        for(int j=i+p[i]; j<len; j++)
        {
            if(j-p[j]+1 <= i+p[i])
            {
                int x = j - ( i + p[i] );
                int last = (len + j + x) >> 1;
                if(last <= len - 2 && last + p[last] == len)
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", s);
        palindrome();
        if (solve()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值