D - 楼下水题(kmp+Manacher)
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)的更多相关文章
- B - 楼下水题(扩展欧几里德)
B - 楼下水题 Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- poj-2406(kmp水题)
题意:定义一个a*b=字符串a连接字符串b:给你一个字符串s,问你这个字符串最多能用多少个字符串t连接得到:例如:aaaa=4个a构成: 解题思路:kmp水题,next数组除了查找字串以外最广泛的一种 ...
- POJ 3641 Oulipo KMP 水题
http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...
- POJ 水题(刷题)进阶
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)
In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches f ...
- UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)
题目链接:点击打开链接 题意:给你一个串,让你在串后面添加尽可能少的字符使得这个串变成回文串. 思路:这题可以kmp,manacher,后缀数组三种方法都可以做,kmp和manacher效率较高,时间 ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
随机推荐
- 原生js判断某个元素是否有指定的class名的几种方法
[注意]以下方法只对class只有一个值的情况下操作 ************************************************************* 结构部分: <d ...
- std中map
在map中需要对位置a和b值进行交换,代码如下: auto val1 = tmpMap.at(a); auto val2 = tmpMap.at(b); tmpMap.insert(std::make ...
- apache hide index.php
<Directory "D:/usr/local/www"> AllowOverride all Options +FollowSymLinks +SymL ...
- MyEclipseアンロックの手順
↓ ↓ ↓ ↓ ↓ ↓
- 一周学会Mootools 1.4中文教程:(6)动画
先看一下动画的参数设置: 参数: fps - (number:默认是50) 每秒的帧数. unit - (string:默认是 false) 单位,可为 'px','em',或 '%'. link - ...
- bzoj 2542: [Ctsc2001]终极情报网 费用流
题目链接 2542: [Ctsc2001]终极情报网 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 321 Solved: 125[Submit][S ...
- 7_Table Views
7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...
- DOCTYPE声明的几种类型
DOCTYPE声明的几种类型 DOCTYPE 声明决定着浏览器怎么去解析和渲染当前页面,所以对于页面来说是很重要的. HTML5时代,统一用 <!DOCTYPE html> 这样简单的方式 ...
- win7使用的一些误区以及困惑
总结了一些新人在使用win7时容易产生的误区和困惑,罗列出来说明一下,以便新人能尽快适应新的操作系统. 1.内存使用的问题:这是个大误区,很多人都用xp时代的眼光来审视win7,这是错误的,因为两者的 ...
- java常用系统包介绍
java.applet提供创建 applet 所必需的类和 applet 用来与其 applet 上下文通信的类.java.awt包含用于创建用户界面和绘制图形图像的所有类.java.awt.colo ...