URAL 1297 Palindrome 最长回文子串
POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher。贴一下代码
两个小错,一个是没弄懂string类的substr的用法是S.substr(i,len)从i开始的长度为len的一段。另外一个是RMQ的时候,询问rk[i],rk[j]的最长前缀应该是等效于求lcp[rk[i]] lcp[rk[j]-1]这一段,这个要在询问的时候注意一下。
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#define maxn 1000050
using namespace std; int sa[maxn + 50];
int lcp[maxn + 50];
int rk[maxn + 50];
int tmp[maxn + 50];
int d[maxn + 50][25];
int n, k;
bool cmp_sa(int i, int j)
{
if (rk[i] != rk[j]) return rk[i] < rk[j];
else{
int ri = i + k <= n ? rk[i + k] : -1;
int rj = j + k <= n ? rk[j + k] : -1;
return ri < rj;
}
} void construct_sa(string S, int *sa)
{
n = S.length();
for (int i = 0; i <= n; i++){
sa[i] = i;
rk[i] = i < n ? S[i] : -1;
}
for (k = 1; k <= n; k <<= 1){
sort(sa, sa + n + 1, cmp_sa);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++){
tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++){
rk[i] = tmp[i];
}
}
} void construct_lcp(string S, int *sa, int *lcp)
{
n = S.length();
for (int i = 0; i <= n; i++) rk[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n; i++){
int j = sa[rk[i] - 1];
for (h ? h-- : 0; i + h < n&&j + h < n&&S[i + h] == S[j + h]; h++);
lcp[rk[i] - 1] = h;
}
} void construct_rmq(int *lcp,int sizen)
{
for (int i = 0; i <= sizen; i++) d[i][0] = lcp[i];
for (int j = 1; (1 << j) <= sizen; j++){
for (int i = 0; (i + (1 << j) - 1) <= sizen; i++){
d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
}
}
} int rmq_query(int l, int r)
{
if (l > r) swap(l, r); r -= 1;
int k = 0; int len = r - l + 1;
while ( (1 << (k + 1)) < len) k++;
return min(d[l][k], d[r - (1 << k) + 1][k]);
} string S;
string T; int main()
{
int ca = 0;
while (cin >> S)
{
if (S == "END") break;
int ctr = S.length();
T = S;
reverse(T.begin(), T.end());
S += '#' + T;
construct_sa(S, sa);
construct_lcp(S, sa, lcp);
construct_rmq(lcp, S.length() + 1);
int ans = 0;
string ansString;
int ansid;
int anstype;
for (int i = 0; i < ctr; i++){
int j = 2 * ctr - i;
int l = rmq_query(rk[i], rk[j]);
if (2 * l - 1>ans){
ans = 2 * l - 1;
ansid = i;
anstype = 0;
}
}
for (int i = 1; i < ctr; i++){
int j = 2 * ctr - i + 1;
int l = rmq_query(rk[i], rk[j]);
if (2 * l > ans){
ansid = i;
anstype = 1;
ans = 2 * l;
}
}
if (anstype == 0){
//ansString = S.substr(ansid-(ans-1)/2, ansid + (ans - 1) / 2);
for (int i = ansid - (ans - 1) / 2; i <= ansid + (ans - 1) / 2; i++){
cout << S[i];
}
cout << endl;
}
else{
//ansString = S.substr(ansid - ans / 2, ansid + ans / 2);
for (int i = ansid - ans/ 2; i <= ansid + ans/ 2-1; i++){
cout << S[i];
}
cout << endl;
}
}
return 0;
}
URAL 1297 Palindrome 最长回文子串的更多相关文章
- URAL 1297 求最长回文字符串
有种简单的方法,数组从左到右扫一遍,每次以当前的点为中心,只要左右相等就往左右走,这算出来的回文字符串是奇数长度的 还有偶数长度的回文字符串就是以当前扫到的点和它左边的点作为中心,然后往左右扫 这是O ...
- 【URAL 1297】Palindrome 最长回文子串
模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...
- Ural 1297 Palindrome 【最长回文子串】
最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...
- Ural 1297 Palindrome(后缀数组+最长回文子串)
https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...
- URAL 1297 最长回文子串(后缀数组)
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297
1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a ...
- ural 1297 后缀数组 最长回文子串
https://vjudge.net/problem/URAL-1297 题意: 给出一个字符串求最长回文子串 代码: //论文题,把字符串反过来复制一遍到后边,中间用一个没出现的字符隔开,然后就是枚 ...
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
随机推荐
- WP开发笔记——程序的退出方法
Windows Phone程序中,并没有之前的类似于“App.Exit()”之类的函数用来让你退出程序.这是怎么回事儿呢? 很简单,在Windows Phone 7中系统要求配备了硬件的“Back”键 ...
- winform Config文件操作
using System;using System.Collections.Generic;using System.Text;using System.Xml;using System.Config ...
- vc列表控件的初始化
void CManageProcessDlg::InitList() { m_ListProcess.SetExtendedStyle(m_ListProcess.GetExtendedStyle( ...
- 在HTML中通过jQuery设置列表项符号
在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...
- 【转载】 使用Anemometer基于pt-query-digest将MySQL慢查询可视化
原文地址:使用Anemometer基于pt-query-digest将MySQL慢查询可视化 作者:84223932 本文主要介绍使用Anemometer基于pt-query-digest将MySQL ...
- gcc编译出现的问题
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error 解决办法:g++ -std=c++11
- 游刃于MVC、WCF中的Autofac
为了程序的健壮性.扩展性.可维护性,依赖抽象而不是具体实现类等等,于是我选择了Autofac依赖注入容器 就是这个工厂来降低耦合.之前买东西是自己去超市,现在呢 我需要什么东西,他们给送过来直接拿到了 ...
- WPF TextBox 的 EventTrigger & 重写控件
遇到一个需求,在textbox获得焦点的时候,调用一个外部的软键盘. 这可以用两个不同的方法来达到目的. 1.EventTrigger 首先定义一个Style <Style x:Key=&quo ...
- 0x04 高级语法
while-endw .while(条件) 循环体(条件满足时执行) .endw repeat-until .repeat 循环体(条件不满足时执行) .until(条件) if-elseif-end ...
- 树形动规--没有上司的舞会--C++
题目来源:code[VS] 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个 ...