题目链接:

题意:给定一个只含字母的字符串,求在字符串末尾添加尽量少的字符使得字符串为回文串。

思路:因为只能从末尾添加字符,所以其实求的是最长的后缀回文串。那么添加的字符为除了这个原串的最长后缀回文串之外的其他字符。于是问题就转变成了求字符串的最长后缀回文串,对于后缀数组求回文串子串的做法,将整个字符串反过来写在原字符串后面,中间用一个特殊的字符隔开。这样就把问题变为了求这个新的字符串的某两个后缀的最长公共前缀。奇数长度和偶数长度的分开做。对于求奇数长度,假设现在枚举的位置为i,那么对应在反过来的串的位置为2*len-i。[len为输入字符串的长度,字符串的下标从0开始],求i和2*len-i的最长公共前缀,如果长度等于len-i那么就说话后缀i是回文后缀。 同理偶数长度的位置是i和2*len-i+1。关于求任意两个后缀的最长公共前缀可以用预处理+RMQ的做法。总的复杂度为O(nlogn)

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
#include<set>
using namespace std;
typedef long long int LL;
const int MAXN = * + ;
int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for (i = ; i < m; i++) WS[i] = ;
for (i = ; i < n; i++) WS[x[i] = r[i]]++;
for (i = ; i < m; i++) WS[i] += WS[i - ];
for (i = n - ; i >= ; i--) sa[--WS[x[i]]] = i;
for (j = , p = ; p < n; j *= , m = p)
{
for (p = , i = n - j; i < n; i++) y[p++] = i;
for (i = ; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ; i < n; i++) wv[i] = x[y[i]];
for (i = ; i < m; i++) WS[i] = ;
for (i = ; i < n; i++) WS[wv[i]]++;
for (i = ; i < m; i++) WS[i] += WS[i - ];
for (i = n - ; i >= ; i--) sa[--WS[wv[i]]] = y[i];
for (t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i++)
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
return;
}
int Rank[MAXN], height[MAXN], sa[MAXN];
void calheight(int *r, int *sa, int n){
int i, j, k = ;
for (i = ; i <= n; i++) { Rank[sa[i]] = i; }
for (i = ; i < n; height[Rank[i++]] = k){
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; k++);
}
return;
}
int RMQ[MAXN], mm[MAXN], best[][MAXN];
void initRMQ(int n){
int i, j, a, b;
for (mm[] = -, i = ; i <= n; i++)
mm[i] = ((i&(i - )) == ) ? mm[i - ] + : mm[i - ];
for (i = ; i <= n; i++) best[][i] = i;
for (i = ; i <= mm[n]; i++)
for (j = ; j <= n + - ( << i); j++)
{
a = best[i - ][j];
b = best[i - ][j + ( << (i - ))];
if (RMQ[a]<RMQ[b]) best[i][j] = a;
else best[i][j] = b;
}
return;
}
int askRMQ(int a, int b){
int t;
t = mm[b - a + ]; b -= ( << t) - ;
a = best[t][a]; b = best[t][b];
return RMQ[a]<RMQ[b] ? a : b;
}
int lcp(int a, int b){
int t;
a = Rank[a]; b = Rank[b];
if (a>b) { t = a; a = b; b = t; }
return(height[askRMQ(a + , b)]);
}
char str[MAXN];
int r[MAXN], len, lenstr;
void solve(){
int plr = ;
for (int i = ; i < lenstr; i++){ //奇长度回文
int idx = * lenstr - i;
if (idx<len&&lcp(i, idx) >= lenstr - i){
plr = max(plr, (lenstr - i) * - );
}
}
for (int i = ; i < lenstr; i++){ //偶长度回文
int idx = * lenstr - i + ;
if (idx<len&&lcp(i, idx) >= lenstr - i){
plr = max(plr, (lenstr - i) * );
}
}
printf("%s", str);
for (int i = lenstr - - plr; i >= ; i--){
printf("%c", str[i]);
}
printf("\n");
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (~scanf("%s", str)){
lenstr = strlen(str); len = lenstr;
for (int i = ; i < lenstr; i++){
r[i] = str[i];
}
r[len++] = ;
for (int i = lenstr - ; i >= ; i--){
r[len++] = str[i];
}
r[len] = ;
da(r, sa, len + , );
calheight(r, sa, len);
for (int i = ; i <= len; i++){ RMQ[i] = height[i]; }
initRMQ(len);
solve();
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

思路二:本题也可以用KMP的做法,由于只能在末尾添加字符所以可以用反过来的串和原串进行匹配,看看最后能匹配多次就是最长后缀回文的长度了。对于求回文串不仅仅可以用后缀数组和KMP,HASH,manacher,回文树都是可行的办法,不过这题还是KMP比较容易写而且运行时间明显比后缀数组快

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
#include<set>
using namespace std;
typedef long long int LL;
const int MAXN = * + ;
char str[MAXN], restr[MAXN];
int Next[MAXN], len;
void getNext(char *s, int len){
int i = , j = -;
memset(Next, , sizeof(Next));
Next[] = -;
while (i<len){
if (j == - || s[i] == s[j]){
i++; j++;
Next[i] = j;
}
else{
j = Next[j];
}
}
}
int KMP(char *str, char *restr, int len){
int j = ;
for (int i = ; i<len; i++){
while (j != - && restr[j] != str[i]){
j = Next[j];
}
if (restr[j] == str[i]){
j++;
}
if (j == -){
j = ;
}
}
return j;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (scanf("%s", str) != EOF){
len = strlen(str);
for (int i = len - ; i >= ; i--){
restr[len - i - ] = str[i];
}
getNext(restr, len);
printf("%s", str);
for (int i = KMP(str, restr, len) ; i<len; i++){
printf("%c", restr[i]);
}
printf("\n");
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

UVA 11475 后缀数组/KMP的更多相关文章

  1. hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?

    题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...

  2. POJ 3450 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...

  3. hdu 5769 Substring 后缀数组 + KMP

    http://acm.hdu.edu.cn/showproblem.php?pid=5769 题意:在S串中找出X串出现的不同子串的数目? 其中1 <= |S| < $10^5$ 官方题解 ...

  4. UVA - 10298 后缀数组(仅观赏)

    题意:求最小循环节 \(KMP\)可以20ms通过,而\(da\)实现的后缀数组并无法在3000ms内通过 听说要用\(dc3\)才勉强卡过,这里仅列出\(da\)实现 #include<ios ...

  5. poj 2406 Power Strings (后缀数组 || KMP)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28859   Accepted: 12045 D ...

  6. POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  7. BZOJ 3796 后缀数组+KMP

    思路: 写得我头脑发蒙,,, 旁边还有俩唱歌的 抓狂 (感谢lh大爷查错) 首先 1.w是s1的子串 2.w是s2的子串 这两步很好办啊~ 后缀数组一下O(n)就可以搞 重点是 这个:3.s3不是w的 ...

  8. BZOJ3796 Mushroom追妹纸(二分答案+后缀数组+KMP)

    求出一个串使得这个串是\(s1,s2\)的子串.串中不包含\(s3\). 如果没有这个\(s3\)就可以二分答案,然后height小于二分值分一组.看看每组里是不是出现过\(s1,s2\)的后缀.判断 ...

  9. UVa 11107 (后缀数组 二分) Life Forms

    利用height值对后缀进行分组的方法很常用,好吧,那就先记下了. 题意: 给出n个字符串,求一个长度最大的字符串使得它在超过一半的字符串中出现. 多解的话,按字典序输出全部解. 分析: 在所有输入的 ...

随机推荐

  1. NEFU 1112 粉刷栅栏算法

    题目链接 中文题 简单搜索题 例数据 输入 6 1 1 1 1 9 9 输出 3 注意是每一个递归搜索都返回一个min 而不是只有总的返回min #include <cstdio> #in ...

  2. 编译QtAV工程库

    去https://github.com/wang-bin/QtAV下载源代码 去https://sourceforge.net/projects/qtav/files/depends/QtAV-dep ...

  3. [Android Pro] Android性能优化典范第一季

    reference to : http://www.cnblogs.com/hanyonglu/p/4244035.html#undefined 2015年伊始,Google发布了关于Android性 ...

  4. September 21st 2016 Week 39th Wednesday

    Don't try so hard, the best things come when you least expect them. 不要着急,最好的总会在最不经意的时候出现. Always tur ...

  5. webstorm配置scss自动编译路径

    webstorm支持sass的同步编译,也就是即写即编译,并且可以指定编译后的css的目录. 本文使用的webstorm为8.0版本 scss安装:http://www.w3cplus.com/sas ...

  6. 简易qq对话框

    //本程序由QT5 creator编译可运行 //dialog.h 1 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> class ...

  7. 【转】VS项目属性的一些配置项的总结

    首先,解决方案和项目文件夹包含关系(c++项目): VS解决方案和各个项目文件夹以及解决方案和各个项目对应的配置文件包含关系,假设新建一个项目ssyy,解决方案起名fangan,注意解决方案包括项目, ...

  8. 解决git客户端MINGW32下的“Could not open a connection to your authentication agent.”

    使用git, 下载客户端后想进行和github 进行ssh 互通 出现以下情况: hadoop@deng-PC MINGW32 ~/.ssh$ ssh-add ~/.ssh/id_rsaCould n ...

  9. 理解KMP算法

    母串:S[i] 模式串:T[i] 标记数组:Next[i](Next[i]表示T[0~i]最长前缀/后缀数) 先来讲一下最长前缀/后缀的概念 例如有字符串T[6]=abcabd接下来讨论的全部是真前缀 ...

  10. web项目没有run on server时..

    文章转载至:http://blog.csdn.net/hongchangfirst/article/details/7722703 web项目没有run on server 1.首先确保正确安装Tom ...