题意:给定一个字符串,求一个最长的回回文子串,多解输出第一个。

析:把字符串翻转然后放到后面去,中间用另一个字符隔开,然后枚举每一个回文串的的位置,对第 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)的更多相关文章

  1. URAL 1297 Palindrome 后缀数组

    D - Palindrome Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  2. URAL - 1297 Palindrome —— 后缀数组 最长回文子串

    题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...

  3. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

    题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...

  4. POJ 3693 后缀数组+RMQ

    思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  5. 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 ...

  6. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  7. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  8. URAL 1297 Palindrome(后缀数组+ST表)

    [题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...

  9. 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 ...

随机推荐

  1. STL讲解报告

    三十分钟掌握STL STL概述 STL的一个重要特点是数据结构和算法的分离.尽管这是个简单的概念,但这种分离确实使得STL变得非常通用.例如,由于STL的sort()函数是完全通用的,你可以用它来操作 ...

  2. c#学习内容

    学习winform+DevExpress 界面制作 wpf UIAutomation 控制别的程序 ok c#通过句柄控制别的程序  ok c# 截图  ok c# 多线程  ok c# 数据库myq ...

  3. MySQL 高可用架构在业务层面的应用分析

    MySQL 高可用架构在业务层面的应用分析 http://mp.weixin.qq.com/s?__biz=MzAxNjAzMTQyMA==&mid=208312443&idx=1&a ...

  4. 推荐ajaxfilemanager for tiny_mce 比较完善的tiny_mce编辑器的图片上传及图片管理插件PHP版 支持中文

    tiny_mce编辑器,我觉得挺简洁.好用的,但就是图片上传的插件是收费的,而且网上找了半天也没有找到开源好用的上传插件. 不过功夫不负有心人,终于还就被我找到一款相当满意的插件. 这个插件的名字叫a ...

  5. HDU 4267 A Simple Problem with Integers(2012年长春网络赛A 多颗线段树+单点查询)

    以前似乎做过类似的不过当时完全不会.现在看到就有点思路了,开始还有洋洋得意得觉得自己有不小的进步了,结果思路错了...改了很久后测试数据过了还果断爆空间... 给你一串数字A,然后是两种操作: &qu ...

  6. BZOJ 1562 [NOI2009]变换序列:二分图匹配

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1562 题意: 给定n,定义D(x,y) =  min(|x-y|, n-|x-y|),然后 ...

  7. jQuery Cloud Zoom:图片放大镜插件 破解插件

    /* Cloud Zoom 10 Site License (CZ01-10). Version 3.1 rev 1312051822 */ (function(e) { function s(a) ...

  8. Metaspoit的使用

    一.环境的使用和搭建 首先我的攻击机和靶机都搭建在虚拟机上,选用的是VMware Workstation Pro虚拟机. 攻击机选用的是Linux kali 2017.2版本,而靶机安装的是XP sp ...

  9. ACM学习历程—HDU 5073 Galaxy(数学)

    Description Good news for us: to release the financial pressure, the government started selling gala ...

  10. 让Django支持数据库长连接(可以提高不少性能哦)

    书接上回 上回我们说到:<在生产系统使用Tornado WebServer来代替FastCGI加速你的Django应用> 那么现在很流行用一些高性能的nonblock的app server ...