hdu 5340 最长回文子串变形

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

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

<pre name="code" class="cpp">/**
hdu 5340  最长回文子串变形
题目大意:给定一个字符串问是否正好可以分成三个回文子串(不能为空串)
解题思路:如果直接枚举判断时间复杂度为O(n^3)。可以利用Manacher算法求出所有0~i为回文串和j~n-1为回文串的i和j
           最后枚举这两个位置利用原本求出的r[i],判断中间的是不是回文串即可。复杂度不会超过O(n^2)
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=101005;
char s[maxn],a[maxn*2];
int c1[maxn*2],c2[maxn*2];///0~i满足回文的所有i;j~n-1满足回文的所有j  
int r[maxn*2],len;
/** 
* 求最长回文子串O(n) 
* abaaba 
* i:    0 1 2 3 4 5 6 7 8 9 10 11 12 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)) 
*/  
void Manacher()
{
    int l=0;
    a[l++]='$';
    a[l++]='#';
    for(int i=0; i<len; 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]>mx)
        {
            mx=i+r[i];
            id=i;
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        len=strlen(s);
        Manacher();
        int l1=0,l2=0;
        for(int i=1; i<2*len+2; i++)
        {
            if(r[i]==i&&i!=1)
            {
                c1[++l1]=i-1+r[i];
            }
            if(r[i]==2*len+2-i&&i!=2*len+1)
            {
                c2[++l2]=i-(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]+1;
                int rr=c2[j]-1;
                if(ll>rr)continue;
                int tmp=(rr+ll)/2;
                if(r[tmp]-1==0)continue;
                if(r[tmp]>=(rr-ll+2)/2)
                {
                    flag=0;
                }
            }
        }
        if(flag)puts("No");
        else puts("Yes");
    }
    return 0;
}



                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值