HDOJ 5340 Three Palindromes

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

题目的意思是看一个字符串可以不可以分成三个回文串、

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; int p[44000],n; char str[44444]; void pk() {     int i;     int mx = 0;     int Id;     for(i=1; i<n; i++)     {         if( mx > i )             p[i] = min( p[2*Id-i], mx-i );                 else             p[i] = 1;         for(; str[i+p[i]] == str[i-p[i]]; p[i]++)             ;         if( p[i] + i > mx )         {             mx = p[i] + i;             Id = i;         }     } } char ch[22222]; int p2[22222]; bool dp[22222][4]; int main() {     int T;     scanf("%d",&T);     while(T--)     {         scanf("%s",ch+1);         int len=strlen(ch+1);         int len2=len*2+1;         for(int i=1;i<=len;i++)         {             str[i*2-1]='#';             str[i*2]=ch[i];         }         str[len2]='#';         str[len2+1]='\0';         str[0]='$';         n=len2+1;         pk();         bool ans=false;         for(int i=1;i<=len-2;i++)         {             if(ans)break;             int center=i+1;             if(p[center]-1==i)             {                 for(int j=i+1;j<=len-1;j++)                 {                     if(ans)break;                     int ct=((i+1)*2+j*2)/2;                     if(p[ct]-1>=j-i)                     {                         int nct=len+(j+1);                         if(p[nct]-1>=len-j)ans=true;                     }                 }             }         }         if(ans)printf("Yes\n");         else printf("No\n");     } }

一个时间为(log n)的回文串的算法:https://www.baidu.com/link?url=xBybNJBbPas7PgjpomF-URWaxZ30x3qxSmGJ9nRbf2BBmiqZJ1MiP45hZI72Y_KBQt4W1STz6T7E7hEfFHdC8O0BvEkniYcKd12kg4zXzDW&wd=&eqid=8306f46b0000f6600000000455bd9c27

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wumingiswu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值