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个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
随机推荐
- CSS 组合选择符
CSS 组合选择符 组合选择符说明了两个选择器直接的关系. CSS组合选择符包括各种简单选择符的组合方式. 在 CSS3 中包含了四种组合方式: 后代选取器(以空格分隔) 子元素选择器(以大于号分隔) ...
- java_设计模式_状态模式_State Pattern(2016-08-16)
定义: 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 类图: 状态模式所涉及到的角色有: ● 环境(Context)角色,也成上下文:定义客户端所感兴趣的接口,同时维护一个 ...
- PC10303/UVA10252
一开始看错题啦,以为是最长公共字序列的变题,认真一看,原来是排列后的最长公共序列,本来想着排序后,从小到大共同就输出的,但是认真一想,根本没必要,我有bitmap啊!之后哗啦啦地码完了,发现一个神奇的 ...
- MySQL设置
在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...
- android关于installLocation
以下内容主要参考自官网的描述. 从Android API-8开始,android允许你将应用程序安装到外部存储空间中去(比方:SD卡),你可以在AndroidManifest.xml中添加androi ...
- Make body have 100% of the browser height
Try setting the height of the html element to 100% as well. html, body { height: 100%; } Body looks ...
- ERROR 2003 (HY000): Can't connect to MySQL server
http://blog.csdn.net/longxibendi/article/details/6363934 一.问题的提出 /usr/local/webserver/mysql/bin/mysq ...
- DexIndexOverflowException: Cannot merge new index 66080 into a non-jumbo instruction!
问题 该问题是方法数超过了65536(DEX 64K problem),无法编译成单个dex文件. 解决方案 谷歌官方给出的解决方案 android { compileSdkVersion 21 bu ...
- 微信JS-SDK签名signature错误代码4029
一般是url的错,url需要动态获取 c#的写法: //Request.Url.ToString()这个获取的是没有端口号的有些网站有端口号 string url = Request.Url.Orig ...
- python学习随笔
1 高阶函数的使用: import math def add(x, y, f): return f(x) + f(y) sq = math.sqrt print add(25, 9,sq) 2. ma ...