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 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
/**
hdu 5340  最长回文子串变形
题目大意:给定一个字符串问是否正好可以分成三个回文子串(不能为空串)
解题思路:如果直接枚举判断时间复杂度为O(n^3)。可以利用Manacher算法求出所有0~i为回文串和j~n-1为回文串的i和j
           最后枚举这两个位置利用原本求出的r[i],判断中间的是不是回文串即可。复杂度不会超过O(n^2)
*/
#include<iostream>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=110010;
/**
* 求最长回文子串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))
*/

char a[maxn*2],s[maxn];
int r[maxn*2];
int c1[20011],c2[20011];///0~i满足回文的所有i;j~n-1满足回文的所有j

void Manacher(char s[],int n)
{
    int l=0;
    a[l++]='$';
    a[l++]='#';
    for(int i=0; i<n; 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);
        int n=strn(s);
        Manacher(s,n);
        int l1=0,l2=0;
        for(int i=1; i<2*n+2; i++)
        {
            if(r[i]==i && i!=1)
            {
                l1++;
                c1[l1]=r[i]-1;
            }
            if(r[i]==2*n+2-i && i!=2*n+1)
            {
                l2++;
                c2[l2]=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]*2+2;
                int rr=2*n+2-(c2[j]*2+2);
                int tmp=(ll+rr)/2;
                if(ll>rr) continue;
                if(r[tmp]-1==0) continue;
                if(r[tmp]>=(rr-ll+2)/2)
                {
                    flag=0;
                }
            }
        }
        if(flag)puts("No");
        else puts("Yes");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 08:58:43

hdu 5340 最长回文子串变形的相关文章

hdu5371 最长回文子串变形(Manacher算法)

http://acm.hdu.edu.cn/showproblem.php? pid=5371 Problem Description Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence. Let's define N-sequence, which is composed with three parts and satisfied with the followin

hdu 4513 最长回文子串

就是在最基础的回文子串中多加个判断条件就行 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int num1[100010],num2[200010]; int min(int a,int b) { return a<b?a:b; } int main() { int T,n,i,j; scanf("%d",&T); while(T

hdu 3068 最长回文子串 TLE

后缀数组+RMQ是O(nlogn)的,会TLE..... 标准解法好像是马拉车,O(n).... 1 #include "algorithm" 2 #include "cstdio" 3 #include "cstring" 4 using namespace std; 5 #define maxn 220020 6 7 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; 8 int rank[maxn],heigh

hdu 3068 最长回文子串 马拉车模板

前几天用后缀数组写过一次这题,毫无疑问很感人的TLE了-_-|| 今天偶然发现了马拉车模板,O(N)时间就搞定 reference:http://acm.uestc.edu.cn/bbs/read.php?tid=3258 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define N 110010 6 char s[N*2],str[N*

HDU 3068 [最长回文子串]

#include<iostream> #include<string> #include<string.h> #include<algorithm> #define MAX_LEN 310000 using namespace std; int p[MAX_LEN]; int findBMstr(string &str){ int maxx=-1; memset(p,0,sizeof(p)); int mx=0,id=0; for(int i=1;i

hdu 3068 最长回文(manacher&amp;最长回文子串)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7317    Accepted Submission(s): 2500 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组

HDU 3068 最长回文 (manacher算法)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9188    Accepted Submission(s): 3159 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组

最长回文子串算法

#1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:"小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?" 小Ho奇怪的问道:"什么叫做最长回文子串呢?" 小Hi回答道:"一个字符串中连续的一

[hdu3068 最长回文]Manacher算法,O(N)求最长回文子串

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题意:求一个字符串的最长回文子串 思路: 枚举子串的两个端点,根据回文串的定义来判断其是否是回文串并更新答案,复杂度O(N3). 枚举回文串的对称轴i,以及回文半径r,由i和r可确定一个子串,然后暴力判断即可.复杂度O(N2). 在上一步的基础上,改进判断子串是否是回文串的算法.记fi(r)=(bool)以i为对称轴半径为r的子串是回文串,fi(r)的值域为{0, 1},显然fi(r)是关于r