Three Palindromes

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

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
 
 
 
题目大意:问是否可以找出三段回文串。
 
题解:
没有进行暴力压位,时间接近超时。但是很侥幸过了。
 
#include<bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=20200;
int pre[maxn*2],suf[maxn*2];
int p[maxn*2];
char str[maxn],trans[maxn*2];
int Transform(){
// memset(p,0,sizeof(p));
memset(pre,0,sizeof(pre));
memset(suf,0,sizeof(suf));
int len=strlen(str);
trans[0]='$';
for(int i=1;i<=2*len;i+=2){
trans[i]='#';
trans[i+1]=str[i/2];
}
trans[2*len+1]='#';
trans[2*len+2]='@';
return 2*len+1;
}
int manacher(){
int len=Transform();
int mx=0,pos=0;
for(int i=1;i<=len;i++){
if(i<mx){
p[i]=min(p[2*pos-i],mx-i);
}else{
p[i]=1;
}
for(;trans[i+p[i]]==trans[i-p[i]];p[i]++);
if(mx<i+p[i]){
mx=i+p[i];
pos=i;
}
}
return len;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",str);
int lens=strlen(str);
if(lens<3){
printf("No\n");continue;
}else if(lens==3){
printf("Yes\n");continue;
}else{
int len= manacher();
for(int i=2;i<len;i++){
if(p[i]==i){
pre[i+p[i]-1]=1;
}
if(p[i]==len-i+1){
suf[i-p[i]+1]=1;
}
}
int flag=0;
for(int i=2;i<len&&(!flag);i++){
for(int j=1;j<p[i]&&(!flag);j++){
if(pre[i-j]&suf[i+j]){
flag=1;
printf("Yes\n");
}
}
}
if(!flag){
printf("No\n");
}
}
}
return 0;
}

  

 
 

HDU 5340——Three Palindromes——————【manacher处理回文串】的更多相关文章

  1. Girls' research - HDU 3294 (Manacher处理回文串)

    题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...

  2. hdu 3294 manacher 求回文串

    感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...

  3. manacher算法——回文串计算的高效算法

    manacher算法的由来不再赘述,自行百度QWQ... 进入正题,manacher算法是一个高效的计算回文串的算法,回文串如果不知道可以给出一个例子:" noon ",这样应该就 ...

  4. HDU 5371 Hotaru's problem (Manacher,回文串)

    题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求[前]和[中]是回文,[中]和[后]是回文.求3段最长为多少?由于平分的关系,所以答案应该是3的倍数. 思路:先Mana ...

  5. HDU 5677 ztr loves substring(回文串加多重背包)

    ztr loves substring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. HDU - 5340 Three Palindromes(manacher算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=5340 题意 判断是否能将字符串S分成三段非空回文串 分析 manacher预处理出前缀和后缀回文的位置, 枚举第 ...

  7. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  8. HDU 3613 Best Reward(manacher求前、后缀回文串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...

  9. Hdu 3294 Girls' research (manacher 最长回文串)

    题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...

随机推荐

  1. 基于Ace Admin 的菜单栏实现

    1.首先是数据库表必然包含以下几个字段Id ,ParnetId,Url,Name等 create table dbo.Module ( Id uniqueidentifier not null con ...

  2. Hibernate常见异常总结

    系统配置 1.没有查找到src目录下的hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateExc ...

  3. 记一次成功部署kolla-ansible ocata版本过程

    1.安装的docker版本 [root@controller ~]# docker --versionDocker version 17.09.1-ce, build 19e2cf6 2.安装的ans ...

  4. [C++11]shared_ptr循环引用导致内存泄露

    1 /* 2 * shared_ptr循环引用导致内存泄露 3 */ 4 5 struct A 6 { 7 shared_ptr<A> ptr; // 改为weak_ptr<A> ...

  5. 关于php缓存技术一些见解

    参考的网站[很重要] ①.模拟高并发: https://blog.csdn.net/yxwb1253587469/article/details/50572927 https://blog.csdn. ...

  6. 黑马学习连接池 druid JdbcTemplate c3p0 池技术

    package cn.itcast.jdbctemplate; import org.junit.Test; import org.springframework.jdbc.core.BeanProp ...

  7. 远程私有库的创建 pod 组件化

    参考:   http://www.cnblogs.com/hs-funky/p/6780203.html http://www.jianshu.com/p/4b63dfbd8be7 http://ww ...

  8. Java实现微信小程序支付(准备)

    Java语言开发微信小程序支付功能: 1.通过https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1路径到官方下载Java的支付SD ...

  9. 【Leetcode】Single Number

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  10. php返回数据格式

    PHP返回HTML代码:     header('Content-type:text/html; charset=utf-8'); PHP返回xml代码:header('content-type: t ...