题目链接:https://vjudge.net/problem/UVA-11475

题意:

给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串。

题解:

实际上是求该字符串的“最长回文后缀”,有多种做法,其中用字符串哈希的方法最方便而且速度最快。

字符串哈希:

从字符串的最后一个字符开始,往前进行计算两个哈希值,其中一个按“先高位后低位”的方法计算,另一个按“先低位后高位”的方法计算。如果在某个位置,两个哈希值相等,那么表明该后缀为回文串,求最长的那个即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; const LL seed = ;
char str[MAXN];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len = strlen(str);
int pos = len-;
LL hash1 = , hash2 = , s = ;
for(int i = len-; i>=; i--)
{
hash1 *= seed, hash1 += str[i];
hash2 += str[i]*s, s *= seed;
if(hash1==hash2)
pos = i-;
}
for(int i = ; i<len; i++) putchar(str[i]);
for(int i = pos; i>=; i--) putchar(str[i]);
putchar('\n');
}
}

KMP:

1. 根据next数组的next[len]即为字符串前缀与后缀的最长匹配(不包括重叠的情况),可以将字符串的逆串接在其前面,中间用一个分割符隔开,得到新串,求出新串的next数组

1.1 逆串放在前面是因为用逆串的前缀去匹配原串的后缀。

1.2 中间加个分隔符是用于防止逆串匹配到逆串那里去,即防止过界。

2. 假设新串的长度为len, 那么next[len]即为可节省的最大长度,因而只需添加len-next[len]个字符即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; char x[MAXN];
int Next[MAXN];
void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} char str[MAXN];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len = strlen(str);
int m = *len+;
for(int i = ; i<len; i++)
{
x[len--i] = str[i];
x[len++i] = str[i];
}
x[len] = '$'; x[m] = ;
get_next(x, m); int pos = len-Next[m]-;
for(int i = ; i<len; i++) putchar(str[i]);
for(int i = pos; i>=; i--) putchar(str[i]);
putchar('\n');
}
}

后缀数组:

1.将逆串接在原串的后面,中间用一个分隔符隔开,得到新串。求出新串的后缀数组。

2.枚举原串在新串中的每一个位置i,该位置代表着新串的一个后缀i,设逆串首字符在原串中的位置为j,同样代表着新串的一个后缀j。加入后缀i与后缀j个最长公共前缀lcp满足:i+lcp==len,即表明最长公共前缀已经延伸到原串的串尾,所以该原串的后缀i为回文串。同样,求出最长的那个即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; bool cmp(int *r, int a, int b, int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[i]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(j = ; j<=n; j <<= )
{
p = ;
for(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<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[y[i]]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = ; x[sa[]] = ;
for(i = ; i<n; i++)
x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++; if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} int dp[MAXN][], mm[MAXN];
void initRMQ(int n, int b[])
{
mm[] = -;
for(int i = ; i<=n; i++)
dp[i][] = b[i], mm[i] = ((i&(i-))==)?mm[i-]+:mm[i-];
for(int j = ; j<=mm[n]; j++)
for(int i = ; i+(<<j)-<=n; i++)
dp[i][j] = min(dp[i][j-], dp[i+(<<(j-))][j-]);
} int RMQ(int x, int y)
{
if(x>y) swap(x, y);
x++;
int k = mm[y-x+];
return min(dp[x][k], dp[y-(<<k)+][k]);
} char str[MAXN];
int main()
{
while(scanf("%s", str)!=EOF)
{
int len = strlen(str);
int n = *len+;
for(int i = ; i<len; i++)
{
r[i] = str[i];
r[n--i] = str[i];
}
r[len] = '$'; r[n] = ;
DA(r, sa, Rank, height, n, );
initRMQ(n, height); int pos = len-;
for(int i = ; i<len; i++)
{
int lcp = RMQ(Rank[i], Rank[len+]);
if(i+lcp==len)
{
pos = i-;
break;
}
}
for(int i = ; i<len; i++) putchar(str[i]);
for(int i = pos; i>=; i--) putchar(str[i]);
putchar('\n');
}
}

UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组的更多相关文章

  1. uva 11475 - Extend to Palindrome(KMP)

    option=com_onlinejudge&Itemid=8&category=506&page=show_problem&problem=2470" ta ...

  2. UVA - 11475 Extend to Palindrome (后缀数组)

    Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) ...

  3. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  4. UVa 11475 - Extend to Palindrome

    題目:給你一個字符串,在後面拼接一部分使得它變成回文串,使得串最短.輸出這個回文串. 分析:KMP,dp.這裡利用KMP算法將串和它的轉置匹配,看結束時匹配的長度就可以. 因為串比较長.使用KMP比较 ...

  5. UVA 11475 Extend to Palindrome hash

    题意: 给出一个字符串,让你往后添加最少的字符,使其成为回文串. 分析: 题目就相当于求后缀字符串为回文串的最长长度,判断回文串要O(n)时间,直接判断肯定不行.我们从后往前枚举,每次字符串与上一个字 ...

  6. UVA 11475 Extend to Palindrome(hash)题解

    题意:问你最少加几个字母使所给串变成回文串. 思路:一开始打算将正序和逆序都hash,然后用提取前缀后缀的方法来找,但是RE了,debug失败遂弃之.后来发现可以直接hash,一边hash一边比较.我 ...

  7. UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)

    题目链接:点击打开链接 题意:给你一个串,让你在串后面添加尽可能少的字符使得这个串变成回文串. 思路:这题可以kmp,manacher,后缀数组三种方法都可以做,kmp和manacher效率较高,时间 ...

  8. 字符串哈希及KMP

    字符串很神奇,因为它在计算机中应用很广泛,就每一个程序都需要用到字符串,所以学好字符串是非常重要的. 接下来就介绍两个字符串的基本操作 1:字符串hash  一种可以查找几个字符串有几个不同的字符串. ...

  9. 字符串数据结构模板/题单(后缀数组,后缀自动机,LCP,后缀平衡树,回文自动机)

    模板 后缀数组 #include<bits/stdc++.h> #define R register int using namespace std; const int N=1e6+9; ...

随机推荐

  1. blind xxe攻击

    最近做啊里的题的时候遇到了 http://hivesec.net/web-security/%E5%85%B3%E4%BA%8Eblind-xxe.html

  2. 使用JDK中的类URL访问HDFS(来自吴超Hadoop)

    package hdfs; import java.io.InputStream; import java.net.URL; import org.apache.hadoop.fs.FsUrlStre ...

  3. shell脚本实现定时重启进程

    ##############################Deploy crontab for yechang ad*******eta restart ###################### ...

  4. Javascript模式(二) 发布者/订阅者模式

    var publisher = { // 订阅者数组 subscribers : { "any" : [] }, // 增加订阅者 on : function(type, fn, ...

  5. python中executemany的使用

    conn = MySQLdb.connect(host = “localhost”, user = “root”, passwd = “password”, db = “myDB”, charset= ...

  6. 编译-O 选项对性能提升作用

    GCC -O 选项 这个选项控制所有的优化等级.使用优化选项会使编译过程耗费更多的时间,并且占用更多的内存,尤其是在提高优化等级的时候. -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. ...

  7. springMVC --@RequestParam注解(后台控制器获取參数)

    在SpringMVC后台控制层获取參数的方式主要有两种,一种是request.getParameter("name"),第二种是用注解@RequestParam直接获取. 1.获取 ...

  8. Lua学习三----------Lua数据类型

    © 版权声明:本文为博主原创文章,转载请注明出处 Lua数据类型 - Lua是动态类型语言,不需要为变量定义类型,只需要为变量赋值 - Lua有8中基本数据类型:nil.boolean.number. ...

  9. NativeBase准备工作

    环境 node>= 4.0 npm>= 3.0 rnpm (only if React Native version < 0.29) ReactNativeCLI  安装及运行 ht ...

  10. 华为P20无敌拍摄能力开放 如何即刻获得?

    在全球专业相机测评机构DXOmark发布的相机评测排行中,华为P20.P20 Pro成功登顶“全球拍照最好智能手机”.P20 Pro综合得分高达109分,P20综合得分102分.“华为并非简单地将第三 ...