D - Palindrome

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).
Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.
So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards.
In addition, it is reasonable to assume that the agent will
be sending a very long message, so John has simply to find the longest
message satisfying the mentioned property.
Your task is to help John Pupkin by writing a program to find the
secret message in the text of a given article. As NPRx8086 ignores white
spaces and punctuation marks, John will remove them from the text
before feeding it into the program.

Input

The input consists of a single line, which contains a string of Latin
alphabet letters (no other characters will appear in the string). String
length will not exceed 1000 characters.

Output

The longest substring with mentioned property. If there are several such strings you should output the first of them.

Sample Input

input output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<cstdio>
#include<algorithm>
using namespace std;
#define Max 100020
char s[Max];
int sa[Max], Rank[Max], height[Max];
int wa[Max], wb[Max], wd[Max]; void build_sa(int n, int m){ // 倍增算法 n为总长度,n=l+1, m为字符范围
int i,j,p,*xy, *x = wa, *y = wb;
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[i]=s[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[x[i]]] = i;
for(j=,p=;p<n;j*=,m=p){
for(p=,i = n-j; i < n; i ++) y[p++] = i;
for(i = ; i < n; i ++) if(sa[i] >= j) y[p ++] = sa[i] - j;
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[y[i]]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[x[y[i]]]] = y[i];
xy=x;x=y;y=xy;
p=;x[sa[]]=;
for(i=;i<n;i++){
x[sa[i]] = (y[sa[i-]]==y[sa[i]]&&y[sa[i-]+j]==y[sa[i]+j])?p-:p++;
}
}
} void getHeight(int n){ // 求height数组。
int i, j, k = ;
for(i = ; i <= n; i ++) Rank[sa[i]] = i;
for(i=;i<n ;height[Rank[i++]]=k)
for(k?k--:,j=sa[Rank[i]-];s[i+k]==s[j+k];k++);
}
//sa[1~~l]为有效值 sa[i]=a则代表排在第 i 位的是第a个后缀。 a属于[0~~l-1] 字符串比较不论长度
//Rank[0~~l-1]是有效值 Rank[i]=b则代表第 i 个后缀排在第b位 b属于[1~~l]
//height[2~~l]是有效值 height[i]=c 则代表排在第 i 位的后缀和排在第i-1的后缀的最长前缀长度是c
int strl=;
int max1(int ans,int num,int i){
if(num>ans) ans=num,strl=i;
return ans;
}
int main(){
int l,i,ans,T,temp,j;
while(scanf("%s",s)!=EOF){
l=strlen(s);
int oldl=l;
s[l]='|';
for(i=l+,j=l-;i<=l+l;i++,j--){
s[i]=s[j];
}
s[i]='\0';
l=*l+;
build_sa(l+,);
getHeight(l);
strl=;
ans=;
for(i=;i<=l;i++){
int a = sa[i-], b = sa[i];
if(a > b)
swap(a, b);
if(a < oldl && b > oldl && a+height[i] == l-b)
{
if(height[i] > ans)
ans = height[i], strl = a;
else if(height[i] == ans && a < strl)
strl = a;
}
}
for(i=strl;i<strl+ans;i++)
printf("%c",s[i]);
cout<<endl; }
return ;
}
/*
xatag,ata
*/

URAL 1297 Palindrome 后缀数组的更多相关文章

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

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

  2. URAL 1297 Palindrome (后缀数组+RMQ)

    题意:给定一个字符串,求一个最长的回回文子串,多解输出第一个. 析:把字符串翻转然后放到后面去,中间用另一个字符隔开,然后枚举每一个回文串的的位置,对第 i 个位置,那么对应着第二个串的最长公共前缀, ...

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

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

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

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

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

  6. Manacher Ural 1297 Palindrome

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  7. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  8. ural 1297. Palindrome

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1297 求最长回文子串 典型的后缀数组的入门题目,但是可以用更简单的方法解决,毕竟数据量比 ...

  9. UVA - 11475 Extend to Palindrome (后缀数组)

    Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) ...

随机推荐

  1. Javascript 装载和执行

    http://coolshell.cn/articles/9749.html http://www.cnblogs.com/cheche/archive/2011/03/06/1971955.html

  2. poj2365---求多边形边长总和

    #include <stdio.h> #include <stdlib.h> #include<math.h> #define pi acos(-1) struct ...

  3. hdu--1800--字典树&&其他

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 根据题意可知:意思是有若干个飞行员,需要在扫帚上练习飞行,每个飞行员具有不同的等级,且等级高的飞 ...

  4. Seinfeld(栈模拟)

    Seinfeld Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. C++ *max_element函数找最大元素 *min_element函数找最小元素 STL算法(转)

    http://blog.sina.com.cn/s/blog_6f3a860501019z1f.html #include<iostream> #include<algorithm& ...

  6. oracle 11g导入导出

    数据的导入 1 将D:\daochu.dmp 中的数据导入 TEST数据库中.   imp system/manager@TEST  file=d:\daochu.dmp    上面可能有点问题,因为 ...

  7. 集合的实现 -- 数据结构与算法的javascript描述 第九章

    集合 集合(set)是一种包含不同元素的数据结构. 集合中的元素称为成员. 集合的两个最重要特性是:首先,集合中的成员是无序的:其次,集合中不允许相同成员存在. code function Set() ...

  8. 简单的CSS网页布局--三列布局

    三列布局其实不难,不过要用到position:absolute这个属性,因为这个属性是基于浏览器而言,左右部分各放在左右侧,空出中间一列来实现三列布局. (一)三列布局自适应 <!DOCTYPE ...

  9. 桦仔 笔记4-徐 模仿灾难发生时还原adventurework数据库 示例 stopat

    1 --模仿灾难发生时还原adventurework数据库 示例 stopat 2 3 BACKUP DATABASE AdventureWorks 4 TO DISK= 'D:\MSSQL\Data ...

  10. [C#参考]事件机制

    还是那个项目,为了降低程序的耦合性,我决定小小的重构一下自己原来的代码,把Socket通信和帧的分析这两部分分别封装成一个类,当然线程没有变,只是封装了一下,为的就是模块测试完容易拼接.这也是我打算降 ...