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[ ...
随机推荐
- 使用泛型对java数组扩容
编写一个通用方法,其功能是将数组扩展到10%+10个元素(转载请注明出处) package cn.reflection; import java.lang.reflect.Array; public ...
- c++实现单例
单例宏: //单件定义宏 //------------------------------------- // 在头文件中申明 // DECLARE_SINGLEOBJ( CSampleClass ) ...
- 如何让对象只在堆或者栈中分配空间ANDC++禁止一个类被继承
在开始之前先来分析一下C++中的new运算符和operator new之间的关联. new:指我们在C++里通常用到的运算符,比如A* a = new A或者调用带参数的构造函数; 对于new来说, ...
- 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程
想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...
- This 关键字和变量作用域
public class Number { int count; public void method01(){ // int count=3; count=3; // t ...
- Spring jdbctemplate学习笔记
/*List<?> config = getDB(" select t.datavalue from sys_config t where t.configid = '15' & ...
- MOQ
MOQ:(Minimum order Quantity) 最低订货数量 MOQ 即最小订购量(最小订单量) 对每个产品设定建议订单量是补货的方法之一.另外要注意订单的有效性,这是由供应商制定的 ...
- POJ 1743 Musical Theme(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, ...
- CF R303 div2 C. Woodcutters
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- java载入XML文件并解析xml
import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentExce ...