POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher。贴一下代码

两个小错,一个是没弄懂string类的substr的用法是S.substr(i,len)从i开始的长度为len的一段。另外一个是RMQ的时候,询问rk[i],rk[j]的最长前缀应该是等效于求lcp[rk[i]]  lcp[rk[j]-1]这一段,这个要在询问的时候注意一下。

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#define maxn 1000050
using namespace std; int sa[maxn + 50];
int lcp[maxn + 50];
int rk[maxn + 50];
int tmp[maxn + 50];
int d[maxn + 50][25];
int n, k;
bool cmp_sa(int i, int j)
{
if (rk[i] != rk[j]) return rk[i] < rk[j];
else{
int ri = i + k <= n ? rk[i + k] : -1;
int rj = j + k <= n ? rk[j + k] : -1;
return ri < rj;
}
} void construct_sa(string S, int *sa)
{
n = S.length();
for (int i = 0; i <= n; i++){
sa[i] = i;
rk[i] = i < n ? S[i] : -1;
}
for (k = 1; k <= n; k <<= 1){
sort(sa, sa + n + 1, cmp_sa);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++){
tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++){
rk[i] = tmp[i];
}
}
} void construct_lcp(string S, int *sa, int *lcp)
{
n = S.length();
for (int i = 0; i <= n; i++) rk[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n; i++){
int j = sa[rk[i] - 1];
for (h ? h-- : 0; i + h < n&&j + h < n&&S[i + h] == S[j + h]; h++);
lcp[rk[i] - 1] = h;
}
} void construct_rmq(int *lcp,int sizen)
{
for (int i = 0; i <= sizen; i++) d[i][0] = lcp[i];
for (int j = 1; (1 << j) <= sizen; j++){
for (int i = 0; (i + (1 << j) - 1) <= sizen; i++){
d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
}
}
} int rmq_query(int l, int r)
{
if (l > r) swap(l, r); r -= 1;
int k = 0; int len = r - l + 1;
while ( (1 << (k + 1)) < len) k++;
return min(d[l][k], d[r - (1 << k) + 1][k]);
} string S;
string T; int main()
{
int ca = 0;
while (cin >> S)
{
if (S == "END") break;
int ctr = S.length();
T = S;
reverse(T.begin(), T.end());
S += '#' + T;
construct_sa(S, sa);
construct_lcp(S, sa, lcp);
construct_rmq(lcp, S.length() + 1);
int ans = 0;
string ansString;
int ansid;
int anstype;
for (int i = 0; i < ctr; i++){
int j = 2 * ctr - i;
int l = rmq_query(rk[i], rk[j]);
if (2 * l - 1>ans){
ans = 2 * l - 1;
ansid = i;
anstype = 0;
}
}
for (int i = 1; i < ctr; i++){
int j = 2 * ctr - i + 1;
int l = rmq_query(rk[i], rk[j]);
if (2 * l > ans){
ansid = i;
anstype = 1;
ans = 2 * l;
}
}
if (anstype == 0){
//ansString = S.substr(ansid-(ans-1)/2, ansid + (ans - 1) / 2);
for (int i = ansid - (ans - 1) / 2; i <= ansid + (ans - 1) / 2; i++){
cout << S[i];
}
cout << endl;
}
else{
//ansString = S.substr(ansid - ans / 2, ansid + ans / 2);
for (int i = ansid - ans/ 2; i <= ansid + ans/ 2-1; i++){
cout << S[i];
}
cout << endl;
}
}
return 0;
}

URAL 1297 Palindrome 最长回文子串的更多相关文章

  1. URAL 1297 求最长回文字符串

    有种简单的方法,数组从左到右扫一遍,每次以当前的点为中心,只要左右相等就往左右走,这算出来的回文字符串是奇数长度的 还有偶数长度的回文字符串就是以当前扫到的点和它左边的点作为中心,然后往左右扫 这是O ...

  2. 【URAL 1297】Palindrome 最长回文子串

    模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...

  3. Ural 1297 Palindrome 【最长回文子串】

    最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...

  4. Ural 1297 Palindrome(后缀数组+最长回文子串)

    https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...

  5. URAL 1297 最长回文子串(后缀数组)

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  6. 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

    1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a ...

  7. ural 1297 后缀数组 最长回文子串

    https://vjudge.net/problem/URAL-1297 题意: 给出一个字符串求最长回文子串 代码: //论文题,把字符串反过来复制一遍到后边,中间用一个没出现的字符隔开,然后就是枚 ...

  8. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

  9. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

随机推荐

  1. db.properties 数据库配置文件

    project.pool.initialPoolSize project.pool.minPoolSize project.pool.maxPoolSize project.db.tablePrefi ...

  2. mongodb gdal 矢量数据格式驱动

    写了个mongodb的gdal driver,放在了github上,如果你需要,欢迎加入mongogis group. 直接的效果是使得QGIS, GeoServer, MapServer, ArcG ...

  3. Python的 is 运算符

    1. is运算符判断的是同一性而不是相等性. #x和y都绑定到同一个列表,而z被绑定在另外一个具有相同数值和顺序的列表上 x = y = [1, 2, 3] z = [1, 2, 3] x == y ...

  4. Debug Intro

    The ABAP Debugger is used tool to execute and analyze programs line by line. Using it we can check t ...

  5. iOS Foundation框架 -2.常用集合类简单总结

    Foundation框架中常用的类有:NSString.NSArray.NSSet.NSDictionary 以及它们对应的子类 NSMutableString.NSMutableArray.NSMu ...

  6. Ubantu 安装 LAMP环境

    1.通过 apt-get update 命令从ubantu软件源中下载可安装软件的列表. 2.通过一条命令可以一次性的安装LAMP apt-get install apache2 php5 mysql ...

  7. 完美解决fixed 水平居中问题

    群里的朋友问的,发现自己没写过:就写了下,原理和网上的fixed上下左右四个角的原理一样! 1.防止页面振动: body{ _background-image: url(about:blank); _ ...

  8. HTML5-WebWorker

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. C# WinForm设置TreeView选中节点

    这里假定只有两级节点,多级方法类似.遍历节点,根据选中节点文本找到要选中的节点.treeView.SelectedNode = selectNode; /// <summary> /// ...

  10. Ubuntu14.04忘记root密码的解决方法

    电脑20多天没用忘记密码了,下面是在网上找到的一个解决办法,其它的和这个也大概相同.因为其中有些缺漏,没能给我解决问题.通过分析最终问题还是解决了,现解决方案的关键点记录一下.希望能方便到其它人. 1 ...