UVA-11475-Extend to Palindrome((扩展)kmp)
来源:互联网 发布:csolfps优化大全 编辑:程序博客网 时间:2024/05/31 19:09
UVA-11475-Extend to Palindrome((扩展)kmp)
题目:
Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse
it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you
passed it on to your inexperienced team-mate. When the contest is almost over, you find out that
that problem still isn’t solved. The problem with the code is that the strings generated are often not
palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing
that the situation is desperate, you decide to simply write some additional code that takes the output
and adds just enough extra characters to it to make it a palindrome and hope for the best. Your
solution should take as its input a string and produce the smallest palindrome that can be formed by
adding zero or more characters at its end.
Input
Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of
upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than
or equal to 100,000.
Output
For each line of input, output will consist of exactly one line. It should contain the palindrome formed
by adding the fewest number of extra letters to the end of the corresponding input string.
Sample Input
aaaa
abba
amanaplanacanal
xyz
Sample Output
aaaa
abba
amanaplanacanalpanama
xyzyx
题意:给定一个字符串,在后面添加最少的字符使其变成回文串。
很多种做法,kmp,扩展kmp,后缀数组都可以,这里给出扩展kmp的做法
原串作S串,翻转串作T串进行扩展kmp,遍历一次extand数组,当extand[i]=i时,回文部分最大,然后输出即可。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cstring>#include <stack>#include <queue>#include <set>#include <cmath>#include <vector>using namespace std;int nxt[100005];int extand[100005];char S[100005];char T[100005];void getnext(){ int s=strlen(T); nxt[0]=s; int t=0; for(t=0;t<s-1&&T[t]==T[t+1];t++); nxt[1]=t; int a=1; for(int k=2;k<s;k++){ int p=a+nxt[a]-1,len=nxt[k-a]; if(k-1+len>=p){ int j= (p-k+1)>0 ? (p-k+1) : 0; while(j+k<s&&T[j+k]==T[j]) j++; nxt[k]=j; a=k; } else nxt[k]=len; }}void getextand(){ getnext(); int s=strlen(S),t=strlen(T); int minlen=min(s,t); int a=0; while(a<minlen&&S[a]==T[a]) a++; extand[0]=a; a=0; for(int k=1;k<s;k++){ int p=a+extand[a]-1,len=nxt[k-a]; if(k-1+len>=p){ int j=max(p-k+1,0); while(j+k<s&&j<t&&S[j+k]==T[j]) j++; extand[k]=j;a=k; } else extand[k]=len; }}int main(){ //freopen("out.txt","w",stdout); while(scanf("%s",S)!=EOF){ int n=strlen(S); for(int i=0;i<n;i++){ T[i]=S[n-1-i]; } T[n]=0; //printf("%s %s\n",S,T); getextand(); int maxn=0; for(int i=0;i<n;i++){ if(extand[i]==n-i){ maxn=extand[i]; break; } } printf("%s%s\n",S,T+maxn); } return 0;}
- UVA-11475-Extend to Palindrome((扩展)kmp)
- Uva 11475 : Extend to Palindrome(Hash或KMP)
- UVA 11475 - Extend to Palindrome(KMP)
- uva 11475 - Extend to Palindrome(KMP)
- UVA 11475 Extend to Palindrome KMP
- UVA 11475 Extend to Palindrome KMP,后缀数组
- UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)
- UVA 11475 Extend to Palindrome(字符hash)
- UVa 11475 - Extend to Palindrome
- UVA11475:Extend to Palindrome(Hash 或 KMP)
- Extend to Palindrome - UVa 11475 哈希
- UVa 11475-Extend to Palindrome JAVA
- UVA 11475 Extend to Palindrome hash
- Extend to Palindrome - UVa 11475 Manacher算法
- UVA - 11475 Extend to Palindrome manacher+贪心
- uva11475 - Extend to Palindrome KMP
- UVA 11475 Extend to Palindrome 后缀数组 LCP
- UVA11475--Extend to Palindrome
- iOS集成支付宝遇到的问题一:Cannot find interface declarationfor'NSObject', superclass of'Base64'
- 【Android M】Monkey命令源码及是否处于monkey测试的判断方法
- 缓存穿透,缓存击穿,缓存雪崩解决方案分析
- test
- cmd命令,最新的装逼利器
- UVA-11475-Extend to Palindrome((扩展)kmp)
- Centos7下配置系统日志备份策略
- SUSE 11 操作系统安装
- nyoj 49 开心的小明(01背包)
- $.extend(true,o,d)与$.extend(o,d) 的区别
- 时间相关类 TimeSpan
- echarts图表日期连续问题
- 16款app源码下载收集
- 关闭SwipeRefreshLayout的下拉刷新