题目大意:RT
 
分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的。
 
线段数+后缀数组代码如下:
===========================================================================================================================================
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = 1e4+; struct SuffixArr
{
int tempx[MAXN], tempy[MAXN], text[MAXN];
int rank[MAXN], sa[MAXN], height[MAXN], sum[MAXN];
int *x, *y, N, MaxId; void GetText(char s[])
{
N = strlen(s)*+, MaxId = ;
x = tempx, y = tempy;
int i;
for(i=; s[i]; i++)
{
text[i] = text[N-i-] = x[i] = x[N-i-] = (int)s[i];
y[i] = i, y[N-i-] = N-i-;
} text[i] = x[i] = , y[i] = i;
text[N-] = x[N-] = , y[N-] = N-; /// debug();
}
bool cmp(int i, int len)
{
if(sa[i]+len > N || sa[i-]+len > N)
return false;
if(y[sa[i]] != y[sa[i-]] || y[sa[i]+len] != y[sa[i-]+len])
return false; return true;
}
void baseSort()
{
for(int i=; i<MaxId; i++)
sum[i] = ;
for(int i=; i<N; i++)
sum[ x[ y[i] ] ] += ;
for(int i=; i<MaxId; i++)
sum[i] += sum[i-];
for(int i=N-; i>=; i--)
sa[ --sum[ x[ y[i] ] ] ] = y[i];
}
void GetSa()
{
baseSort(); for(int len=; len<=N; len<<=)
{
int id = ; for(int i=N-len; i<N; i++)
y[id++] = i;
for(int i=; i<N; i++)if(sa[i] >= len)
y[id++] = sa[i] - len; baseSort();
swap(x, y);
x[ sa[] ] = id = ; for(int i=; i<N; i++)
{
if(cmp(i, len) == true)
x[ sa[i] ] = id;
else
x[ sa[i] ] = ++id;
} MaxId = id + ; if(MaxId >= N)break;
}
}
void GetHeight()
{
for(int i=; i<N; i++)
rank[ sa[i] ] = i; for(int k=, i=; i<N; i++)
{
if(!rank[i])
{
height[] = k = ;
continue;
}
if(k)k--; int pre = sa[ rank[i]- ]; while(text[i+k] == text[pre+k])
k++;
height[rank[i]] = k;
}
/// debug();
} void debug()
{
for(int i=; i<N; i++)
printf("%d: %d\n", i, height[i]);
}
};
struct segmentTree
{
int val[MAXN], L[MAXN], R[MAXN]; void Build(int root, int l, int r, int p[])
{
L[root] = l, R[root] = r; if(l == r)
{
val[root] = p[l];
return ;
} int Mid = (l+r)>>; Build(root<<, l, Mid, p);
Build(root<<|, Mid+, r, p); val[root] = min(val[root<<], val[root<<|]);
}
int Query(int root, int u, int v)
{ if(L[root] == u && R[root] == v)
return val[root]; int Mid = (L[root]+R[root]) / ; if(v <= Mid)
return Query(root<<, u, v);
else if(u > Mid)
return Query(root<<|, u, v);
else
return min(Query(root<<, u, Mid), Query(root<<|, Mid+, v));
}
}; SuffixArr suf;
segmentTree seg;
char s[MAXN]; int main()
{
while(scanf("%s", s) != EOF)
{
int len = strlen(s)+; suf.GetText(s);
suf.GetSa();
suf.GetHeight(); seg.Build(, , suf.N-, suf.height); int MaxLen=, k=, x, y; for(int i=; i<len-; i++)
for(int j=; j<; j++)
{
if(j==)
x = suf.rank[len*-i-];
else
x = suf.rank[len*-i-]; if(i== && j)continue; y = suf.rank[i+]; if(x > y)swap(x, y); int m = seg.Query(, x+, y) * + j; if(MaxLen < m)
{
MaxLen = m;
k = i-(m+j)/+;
}
} s[k+MaxLen] = ; printf("%s\n", s+k);
} return ;
}

Palindrome - URAL - 1297(求回文串)的更多相关文章

  1. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...

  2. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. [LeetCode] Find the Closest Palindrome 寻找最近的回文串

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  5. manacher算法,求回文串

    用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...

  6. Misha and Palindrome Degree CodeForces - 501E (回文串计数)

    大意: 给定字符串, 求多少个区间重排后能使原串为回文串. 先特判掉特殊情况, 对于两侧已经相等的位置之间可以任意组合, 并且区间两端点至少有一个在两侧相等的位置处, 对左右两种情况分别求出即可. # ...

  7. hdu 3294 manacher 求回文串

    感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...

  8. hihocoder 1032 manachar 求回文串O(n)

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  9. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

随机推荐

  1. js文件缓存之版本管理

    以前也做过不少项目,但从来就没有把关注的目光投向过js文件缓存.最近终于在毫无意识的情况下跳进了这个大坑. 近几个月来的工作是一个交易系统持续改进项目,迭代发布周期大约为2~3周.最近一次迭代是V16 ...

  2. IOS-图片上传到服务器

    //获取document 路径- (NSString *)getDocumentPath{    NSArray *paths = NSSearchPathForDirectoriesInDomain ...

  3. Ubuntu系列Crontab日记记录

    修改rsyslog文件,将/etc/rsyslog.d/50-default.conf 文件中的#cron.*前的#删掉 重启rsyslog服务service rsyslog restart 重启cr ...

  4. a-b(高精度)

    我现在已经是才语言中的一员了,我在此献上今日的佳作——a-b(高精度),以下是我的程序及其注释,欢迎各位来观赏,耶! 程序: #include<stdio.h> #include<s ...

  5. centos7 玩aapt 安卓应用apk解包工具的安装

    最近在做一个应用市场的项目,需要在centos7下面对apk解包读取其信息,这就想到了使用Google的解包工具aapt,但是由于中国的原因,国内访问原生工具的地址就有些麻烦,这里就贴出地址:http ...

  6. 细介Nigix配置与反向代理

    Nginx(发 音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下 发行.由俄罗斯的程序设计师Igor S ...

  7. javaScript常用方法整合(项目中用到过的)

    防止输入空格.缩进等字符: function trim(str){ return str.replace(/^\s+|\s+$/g,""); } JS去掉style样式标签 fun ...

  8. 使用jQuery播放/暂停 HTML5视频

    文章来自:http://blog.okbase.net/jquery2000/archive/4485.html 我尝试用jQuery控制HTML5视频,两个视频分别在两个tab中,我希望点中tab后 ...

  9. apache 网址重定向

    参考了以下网站,终于基本搞定b2c网站伪静态.剩下的就是体力活了. 回家后整理下. http://yp.oss.org.cn/software/show_resource.php?resource_i ...

  10. [BZOJ 3564] [SHOI2014] 信号增幅仪 【最小圆覆盖】

    题目链接:BZOJ - 3564 题目分析 求最小椭圆覆盖,题目给定了椭圆的长轴与 x 轴正方向的夹角,给定了椭圆长轴与短轴的比值. 那么先将所有点旋转一个角度,使椭圆长轴与 x 轴平行,再将所有点的 ...