hdu5340 Three Palindromes

题目描述:

判断是否能将字符串S分成三段非空回文串。

解题思路:

源代码:

#include <cstdio>
#include <algorithm>
#define MAXN 20010
using namespace std;

int n;
char d[MAXN];///原始字符串
char st[MAXN*2];///经过manacher处理之后的字符串
int p[MAXN*2];///保存回文串半径,ps每个回文串长度一定为奇数

int ll[MAXN*2];///第一个回文串可能的所有半径
int rr[MAXN*2];///第三个回文串可能的所有半径

void manacher(){///manacher算法,可以去查询了解一下
    int i;
    st[0]='$';
    st[1]='#';
    for(i=1;d[i]!='\0';++i){
        st[i*2]=d[i];
        st[i*2+1]='#';
    }
    st[i*2]='\0';
    n=i*2-1;
    int MaxId=0,id;
    for(int i=1;i<=n;i++)
    {
        if(MaxId>i)
            p[i]=min(p[2*id-i],MaxId-i);
        else
            p[i]=1;
        while(st[i+p[i]]==st[i-p[i]])
            p[i]++;
        if(p[i]+i>MaxId){
            id=i;
            MaxId=p[i]+i;
        }
    }
}

int main(){
    int res;
    scanf("%d",&res);
    while(res--){
        scanf("%s",&d[1]);
        manacher();
        int l=0,r=0;
        for(int i=1;i<=n;i++){
            if(p[i]==i&&i!=1)///p[i]==i保证p[i]可以作为第一个回文串的半径,加入ll数组,i!=1保证第一个回文串不为空
                ll[l++]=p[i];
            if(p[i]+i-1==n&&i!=n)///与上面类似
                rr[r++]=p[i];
        }
        int i,j;
        for(i=l-1;i>=0;--i){
            for(j=0;j<=r-1;++j){///枚举第一个回文串和第三个回文串
                int tl=2*ll[i];///第二个字符串的开始位置
                int tr=n+1-2*rr[j];///第二个字符串的结束位置
                int tmp=(tl+tr)/2;///第二个字符串的中间位置,ps:这三个字符串都为奇数,可以自己想想
                if(tl>tr) continue;
                if(p[tmp]==1) continue;///第二个字符串为"#",也即是在原串中为空
                if(p[tmp]*2-1>=tr-tl+1)///以tmp为中点的回文串的长度大于等于第二个字符串的长度,符合题意跳出循环
                    break;
            }
            if(j<=r-1)
                break;
        }
        if(i>=0)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

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

时间: 2024-11-01 03:47:50

hdu5340 Three Palindromes的相关文章

hdu5340—Three Palindromes—(Manacher算法)——回文子串

Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1948    Accepted Submission(s): 687 Problem Description Can we divided a given string S into three nonempty palindromes? Input F

HDU-5340 Three Palindromes(字符串哈希)

http://acm.hdu.edu.cn/showproblem.php?pid=5340 orz到了新的字符串hash姿势 #include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#define rep(i,l,r) for (int i=l;i<=r;i++)#define maxn 200500#define p 100

HDU5340——Manacher算法——Three Palindromes

http://acm.hdu.edu.cn/showproblem.php?pid=5340 /* Manacher算法:O(n) 实现最长回文子串 算法实现: 先向原字符串中插入一个原来串不存在的字符,一般用'#',再O(n)遍历一遍,用一个数组p[i]来记录以str[i]为中心的回文半径(注意str[i]是新串,长度为2*len+1),mx记录当前已有的回文最长到的位置,假定当前为i,j为i前面已经计算过的数,id为最长的回文的中心,那么可以得到一个方程 p[i] = min(p[2*id-

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbe

Codeforces #316 E Pig and Palindromes DP

// Codeforces #316 E Pig and Palindromes // // 题目大意: // // 给你一张地图,n*m每个点是一个字母,现在从(0,0)出发, // 每次只能往右或者往下走,求走到(n-1,m-1)形成回文串的方法数. // // 解题思路: // // 动态规划.首先.如果起点和终点的字母不相同,那么肯定 // 不能形成回文串,直接输出0.对于能形成回文串.我们设状态 // d(step,i,j)表示走了step步,从第0行走到i行,第n-1行走到j行的 /

UVA11584---Partitioning by Palindromes(dp)

We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For exam- ple, ' racecar ' is a palindrome, but ' fastcar ' is not. A partition of a sequence of char- acters is a list of one or more disjoint non-empt

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i