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. 如何为你的美术妹子做Unity的小工具(三)

    绘制脚本组件监视面板的内容 我们写了一个脚本Test using UnityEngine; using System.Collections; using System.Collections.Gen ...

  2. Linux学习之crontab定时任务

    为当前用户创建cron服务 1.  键入 crontab  -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...

  3. JSTL入门

    在页面最上方引入 -------------------- if语句 8}"> b的值大于8 --------------------- foreach语句 i的值是:${i}

  4. oracle中区分audit_file_dest, background_dump_dest, core_dump_dest, user_dump_dest

    一般在$ORACLE_HOME\admin\{SID}目录下: audit_file_dest                 = /u01/app/oracle/admin/{SID}/adump ...

  5. php基础知识--文件操作

    文件操作 文件 广义角度: 任何一个在磁盘上可以看到的符号(包含真正的文件及文件夹) 狭义角度: 真实存储数据的载体(不包含文件夹, 如doc文件,txt文件等) 文件操作: 对文件的增删改查 文件分 ...

  6. fiddler--firefiox代理

    修改端口:修改后重启才能生效

  7. Linux 查看支持的语言,日期,时间,计算器

    1.查看系统目前支持的语言 echo %LANG 2.查看日历 cal 3.查看日期时间 date 4.计算器 bc

  8. SSD的优势

    谈过SSD的发展历史后,现在我们来讲解下SSD相比传统HDD(机械硬盘)的优势. 相信很多读者只要有听说过SSD,必定都会听到对SSD优点的一个字总结:快! 但这一个字要如何去理解呢?很多人可能还不太 ...

  9. Jsoup代码解读之二-DOM相关对象

    Jsoup代码解读之二-DOM相关对象   之前在文章中说到,Jsoup使用了一套自己的DOM对象体系,和Java XML API互不兼容.这样做的好处是从XML的API里解脱出来,使得代码精炼了很多 ...

  10. ubuntu 12.04下安装和配置kohana 3.3.3 的方法

    一.先到官网下载3.3.3版本的压缩包到/var/www/1117/目录下(提前建好1117的目录)解压 解压好的文件有(applications\modules\system\build.xml\c ...