D - 楼下水题

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

A string is said to be a palindrome if it remains same when read backwards. So, 'abba', 'madam' both are palindromes, but 'adam' is not.

Now you are given a non-empty string S, containing only lowercase English letters. The given string may or may not be palindrome. Your task is to make it a palindrome. But you are only allowed to add characters at the right side of the string. And of course you can add any character you want, but the resulting string has to be a palindrome, and the length of the palindrome should be as small as possible.

For example, the string is 'bababa'. You can make many palindromes including

bababababab

babababab

bababab

Since we want a palindrome with minimum length, the solution is 'bababab' cause its length is minimum.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.

Output

For each case, print the case number and the length of the shortest palindrome you can make with S.

Sample Input

4

bababababa

pqrs

madamimadam

anncbaaababaaa

Sample Output

Case 1: 11

Case 2: 7

Case 3: 11

Case 4: 19

题解:

kmp,找右侧添加若干字母使这个字符串回文;也可以用manacher算法,ans用来找最大回文串长度,Ms用来找从可以到字符串结尾的回文串长度;

少输出了换行没有pe,是wa。。。无耐;

记住要是反转后的串匹配未旋转的串;

anncbaaababaaa

aaababaaacnna

最后的p指向失配的位置9;所以是len+len-j;

代码:

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
char s[MAXN],m[MAXN];
int p[MAXN];
void getp(){
int i=,j=-;
p[]=-;
while(m[i]){
if(j==-||m[i]==m[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
int kmp(){
getp();
// for(int i=0;s[i];i++)printf("%d ",p[i]);puts("");
int i=,j=;
int mx=,temp=;
while(m[i]){
if(j==-||m[j]==s[i]){
i++;j++;
}
else{
j=p[j];
}
}
return j;
}
int main(){
int T,flot=;
scanf("%d",&T);
while(T--){
scanf("%s",s);
strcpy(m,s);
int len=strlen(s);
reverse(m,m+len);
// puts(m);
//puts(s);
printf("Case %d: %d\n",++flot,len+len-kmp());
}
return ;
}

Manacher:

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=;
char str[MAXN],s[MAXN<<];
int p[MAXN<<],Ms;
int Manacher(char *s,int len){
mem(p,);
p[]=p[]=;
int id=,mx=,ans=;
Ms=;
for(int i=;i<len;i++){
if(mx>i)p[i]=min(p[*id-i],mx-i);//2*id-i是对称轴左侧;
else p[i]=;
while(s[i-p[i]]==s[i+p[i]])p[i]++;
if(p[i]+i>mx)mx=p[i]+i,id=i;
ans=max(ans,p[i]);
if(mx==len)Ms=max(Ms,p[i]-);
}
return ans-;
}
int main(){
int T,flot=;
scanf("%d",&T);
while(T--){
scanf("%s",str);
int len=strlen(str);
s[]='@';
for(int i=;i<len;i++){
s[*i+]='#';
s[*i+]=str[i];
}
s[*len+]='#';
int t=Manacher(s,*len+);
if(t==len)printf("Case %d: %d\n",++flot,len);
else printf("Case %d: %d\n",++flot,len+len-Ms);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=1e6+100;
char mstr[MAXN];
int p[MAXN];
char s[MAXN];
void getp(){
int i=0,j=-1;
p[0]=-1;
while(s[i]){
if(j==-1||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
} int kmp(){
getp();
int j=0,i=0;
while(mstr[i]){
if(j==-1||s[j]==mstr[i]){
i++;j++;
}
else j=p[j];
}
return j;
} int main(){
int kase=0,T;
SI(T);
while(T--){
scanf("%s",s);
strcpy(mstr,s);
int len=strlen(s);
reverse(s,s+len);
printf("Case %d: %d\n",++kase,len+len-kmp());
}
return 0;
}

  

D - 楼下水题(kmp+Manacher)的更多相关文章

  1. B - 楼下水题(扩展欧几里德)

    B - 楼下水题 Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  2. poj-2406(kmp水题)

    题意:定义一个a*b=字符串a连接字符串b:给你一个字符串s,问你这个字符串最多能用多少个字符串t连接得到:例如:aaaa=4个a构成: 解题思路:kmp水题,next数组除了查找字串以外最广泛的一种 ...

  3. POJ 3641 Oulipo KMP 水题

    http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...

  4. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  5. ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)

    In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches f ...

  6. UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)

    题目链接:点击打开链接 题意:给你一个串,让你在串后面添加尽可能少的字符使得这个串变成回文串. 思路:这题可以kmp,manacher,后缀数组三种方法都可以做,kmp和manacher效率较高,时间 ...

  7. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  9. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

随机推荐

  1. php-fpm 开启错误日志

    #php-fpm.conf #open catch_workers_output = yes #php.ini log_errors = On error_log=/data/logs/php-fpm ...

  2. Hadoop学习之自定义二次排序

    一.概述    MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往 往有要对reduce输出结果进行二次排 ...

  3. 联想V480关闭UEFI安装Win7

       联想V480关闭UEFI安装Win7 http://www.dadclab.com/archives/3283.jiecao 故事背景 兔兔牛入了一枚Lenovo V480,预装Win8,想换成 ...

  4. spring与hibernate整合配置基于Annotation注解方式管理实务

    1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 <!-- jdbc连接信息 --> <context:property-pla ...

  5. MySQL 二进制日志过滤

    binlog_do_db; binlog_ignore_db; 这个两个参数是用来控制对哪个数据库的更改要记录日志:下面以binlog_ignore_db为例子. 假如binlog_ignore_db ...

  6. Java图形化界面设计——布局管理器之CardLayout(卡片布局)

  7. Nhibernate初入门基本配置(一)

    文章出处:http://www.cnblogs.com/GoodHelper/archive/2011/02/14/nhiberante_01.html 一.NHibernate简介 什么是?NHib ...

  8. Mybaits入门之起航

    前言 Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下. 学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而 ...

  9. 关于javascript面向对象之闭包

    要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量,而在函数外部无法 ...

  10. HDU1171:Big Event in HDU(多重背包分析)

    通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...