URAL 1297 Palindrome (后缀数组+RMQ)
题意:给定一个字符串,求一个最长的回回文子串,多解输出第一个。
析:把字符串翻转然后放到后面去,中间用另一个字符隔开,然后枚举每一个回文串的的位置,对第 i 个位置,那么对应着第二个串的最长公共前缀,
求最长公共子串,可以用RMQ解决。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 10;
const int mod = 1000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Array{
int s[maxn], sa[maxn], t[maxn], t2[maxn];
int h[maxn], r[maxn], c[maxn];
int n;
int dp[maxn][20]; void init(){ n = 0; memset(sa, 0, sizeof sa); }
void build_sa(int m){
int *x = t, *y = t2;
for(int i = 0; i < m; ++i) c[i] = 0;
for(int i = 0; i < n; ++i) ++c[x[i] = s[i]];
for(int i = 1; i < m; ++i) c[i] += c[i-1];
for(int i = n-1; i >= 0; --i) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1){
int p = 0;
for(int i = n-k; i < n; ++i) y[p++] = i;
for(int i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
for(int i = 0; i < m; ++i) c[i] = 0;
for(int i = 0; i < n; ++i) ++c[x[y[i]]];
for(int i = 1; i < m; ++i) c[i] += c[i-1];
for(int i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = 1; x[sa[0]] = 0;
for(int i = 1; i < n; ++i)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
if(p >= n) break;
m = p;
}
} void getHight(){
int k = 0;
for(int i = 0; i < n; ++i) r[sa[i]] = i;
for(int i = 0; i < n; ++i){
if(k) --k;
int j = sa[r[i]-1];
while(s[i+k] == s[j+k]) ++k;
h[r[i]] = k;
}
} void rmq_init(){
for(int i = 1; i <= n; ++i) dp[i][0] = h[i];
for(int j = 1; (1<<j) <= n; ++j)
for(int i = 1; i + (1<<j) <= n; ++i)
dp[i][j] = min(dp[i][j-1], dp[i+(1<<j-1)][j-1]);
} int query(int L, int R){
L = r[L], R = r[R];
if(L > R) swap(L, R);
++L;
int k = int(log(R-L+1) / log(2.0));
return min(dp[L][k], dp[R-(1<<k)+1][k]);
}
};
char s[maxn];
Array arr; int main(){
while(scanf("%s", s) == 1){
n = strlen(s);
arr.init();
for(int i = 0; i < n; ++i) arr.s[arr.n++] = s[i];
arr.s[arr.n++] = 128;
for(int i = n-1; i >= 0; --i) arr.s[arr.n++] = s[i];
arr.s[arr.n++] = 0;
arr.build_sa(130);
arr.getHight();
arr.rmq_init();
int ans = 0, idx;
for(int i = 0; i < n; ++i){
int res = max(arr.query(i, 2*n-i) * 2 - 1, arr.query(i, 2*n-i+1) * 2); // odd
if(res > ans){
ans = res; idx = i;
}
}
if(ans & 1) for(int i = idx-ans/2; i <= ans/2+idx; ++i)
printf("%c", s[i]);
else for(int i = idx-ans/2; i < idx+ans/2; ++i)
printf("%c", s[i]);
printf("\n");
}
return 0;
}
URAL 1297 Palindrome (后缀数组+RMQ)的更多相关文章
- URAL 1297 Palindrome 后缀数组
D - Palindrome Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- URAL - 1297 Palindrome —— 后缀数组 最长回文子串
题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- POJ 3693 后缀数组+RMQ
思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- spoj687 REPEATS - Repeats (后缀数组+rmq)
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...
- URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- URAL 1297 Palindrome(后缀数组+ST表)
[题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...
- Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...
随机推荐
- Android系统篇之—-编写简单的驱动程序并且将其编译到内核源码中【转】
本文转载自:大神 通过之前的一篇文章,我们了解了 Android中的Binder机制和远程服务调用 在这篇文章中主要介绍了Android中的应用在调用一些系统服务的时候的原理,那么接下来就继续来介绍一 ...
- 剑指offer之 旋转数组的最小数字
package Problem8; public class MinInReversingList { /* * 题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. * 输入 ...
- 51nod 1513 && CF570D
题意:给定一棵树,每个节点有一个字母.给定若干个询问,询问某个子树内某一深度的节点是否能将这些节点组合成一个回文串.(深度是以根节点为基准的,不是当前子树根.)数据规模10^5. 神犇题解 子树问题, ...
- STL中流相关的输入输出符和get函数彻底总结:cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
我的总结: [首先:对于流来说,就把流看成一个中转的仓库,对流进行<<运算或>>运算或者get函数的运算都是指,把“流”中的数据“运送”到“内存变量”中去,还是把内存变量中的数 ...
- CDH- 集群时间同步ntp问题解决
在CDH集群中发现有两台机器获取不到心跳(),导致监控不了机器状态,出现告警 可以使用ntpstat检查与ntp 服务器的时间偏差状态 使用 ntpstat 发现没有同步到ntp时间服务器,运行 nt ...
- Windows- 改变cmd控制台默认显示编码
在中国的电脑会要求兼容gbk编码,所以微软进入中国市场也顺应中国的要求,其中最明显的就是在cmd上默认的显示就是GBK .当开发人员在运行一些有打印中文的程序时,由于编码采用国际兼容版本的utf_8等 ...
- Echarts 地图(map)插件之 鼠标HOVER和tooltip自定义数据
在项目开发中,有需要用到地图的地方,百度的echarts地图插件就是个不错的选择, 这里总结一下地图自定义鼠标HOVER时的事件和自定义tooltip数据: 一.鼠标HOVER时的事件: 参照官方文档 ...
- php.ini中的session配置说明
下面介绍能让session运行的必要配置步骤 手动配置PHP运行环境时,最容易遗忘的一项是服务器端session文件的存储目录配置工作,打开php.ini文件,搜索Session,找到session. ...
- php设计模式课程---2、为什么会用到简单工厂设计模式
php设计模式课程---2.为什么会用到简单工厂设计模式 一.总结 一句话总结: 比如调用数据库的语句,如果调用的数据库名字改了,或者调用的数据库类型改了(比如从Mysql用到了Mysqli),那么要 ...
- 常用的Hql语句(转)
原文地址:http://blog.csdn.net/v123411739/article/details/28644007 尊重原创,请访问原文地址 // HQL: Hibernate Que ...