Palindrome - URAL - 1297(求回文串)
#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(求回文串)的更多相关文章
- 马拉车,O(n)求回文串
马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- manacher算法,求回文串
用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...
- Misha and Palindrome Degree CodeForces - 501E (回文串计数)
大意: 给定字符串, 求多少个区间重排后能使原串为回文串. 先特判掉特殊情况, 对于两侧已经相等的位置之间可以任意组合, 并且区间两端点至少有一个在两侧相等的位置处, 对左右两种情况分别求出即可. # ...
- hdu 3294 manacher 求回文串
感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...
- hihocoder 1032 manachar 求回文串O(n)
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
随机推荐
- EditPlus 快捷键
FileFtpUpload Ctrl+Shift+S 上传文件到 FTP 服务器 FileNew Ctrl+N 新建普通的文本文档 Fi ...
- 系统后台图表生成文档说明-javascript
1.引入jquery插件文件datas.js 2.各图表分类 表格 $('#'+tableId).mTable({ url:'', //数据来源,[必填] pageNum:1, //分页,默认为1,[ ...
- 深入理解JAVA多态原理
之前一直知道多态是什么东西,平时敲代码也经常用到多态,但一直没有真正了解多态底层的运行机制到底是怎么样的,这两天才研究明白点,特地写下来,跟各位同学一起进步,同时也希望各位大神指导和指正. 多态的概念 ...
- PoshyTip jQuery 文本提示插件的使用
PoshyTip 是JQuery中一款文本提示插件,在Jsp页面使用相当方便,插件内包含了很多外观样式,可以作为FormTooltips使用. 插件包下载地址:http://vadikom.com/f ...
- Forward reference vs. forward declaration
Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...
- iOS: 学习笔记, 用代码驱动自动布局实例
iOS自动布局是设置iOS界面的利器. 本实例展示了如何使用自动布局语言设置水平布局, 垂直布局 1. 创建空白iOS项目 2. 添加一个控制器类, 修改YYAppDelegate.m文件 #impo ...
- ASP.NET MVC 中使用JavaScriptResult
在浏览器地址栏输入地址,在页面上想通过脚本弹出一个框,看到Controller下有个JavaScript方法,返回的类型是JavaScriptResult,于是想用这个方法弹出框, public Ac ...
- info.plist 属性讲解
1 常用项: Application requires iPhone environment:如果应用程序不能在ipodtouch上运行,设置此项为true; Application usesWi-F ...
- LightOj_1342 Aladdin and the Magical Sticks
题目链接 题意: 地上有n种棍子, 其中有两种类型, 一种类型是可识别, 一种类型是不可识别, 每个棍子都有一个权值. 当你捡到可识别的, 那么你以后就不会再捡这个棍子, 如果是不可识别的, 那么你有 ...
- Struts_json插件配置参数
Struts中使用json需要在struts基础上加上几个包:(这里只列出了重要的几个) commons-lang-2.4.jar: jsonplugin-0[1].32.jar: 下面是配置文件中的 ...