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. OSG坐标系统

    1.世界坐标         世界坐标系描述的是整个场景中所有的对象,可以理解为绝对坐标系,所有对象的位置都是绝对坐标.从整体上考虑,它为所有对象的位置提供一个绝对的参考标准,从而避免了物体之间由于独 ...

  2. SQL Server Primary key 、clustered index 、 unique

    primary key: 1.主键不可以有空值. 2.不可以有重复行. unique : 1.可以有空行. 2.不可以有重复行. clustered index: 1.可以有重复行. 2.可以有空行. ...

  3. Flex整合Spring

    工程需要整合Spring和Flex,在网上众多方法中找到了下面这种,记录留存. 个人认为该方法更适合在已有Spring框架的工程中添加Flex时使用,对原工程内容(主要指配置文件)改动较小. 1.添加 ...

  4. core_cm3.c解析

    CMSIS是Cortex微控制器软件接口标准(Cortex MicroController Software Interface Standard)的缩写,这个是ARM定制的一个用于Cortex-M系 ...

  5. Cortex-M3 .s启动文件分析

    1. 基本概念(CMSIS): Cortex Micro-controller Software Interface Standard,微控制器软件接口标准. 2. CMSIS标准的文件结构: a) ...

  6. Golang网络库中socket阻塞调度源码剖析

    本文分析了Golang的socket文件描述符和goroutine阻塞调度的原理.代码中大部分是Go代码,小部分是汇编代码.完整理解本文需要Go语言知识,并且用Golang写过网络程序.更重要的是,需 ...

  7. JavaMail学习笔记

    适逢计算机网络课程设计,本着挑战自己的态度,选择了一个从未接触的东西:邮箱客户端代理软件的设计.由于对相关协议非常陌生,只能依靠查找资料完成,在学习过程中碰到了一个非常好的博客,故向大家推荐一下. 一 ...

  8. Linux中搭建SVN服务器

    一 下载工具 1. subversion-1.6.17.tar.gz 2. subversion-deps-1.6.17.tar.gz 二 解压两个包: 1.在此之前,我已经创建了一个用户svnroo ...

  9. 网易云课堂_C语言程序设计进阶_第5周:链表_1逆序输出的数列

    1 逆序输出的数列(10分) 题目内容: 你的程序会读入一系列的正整数,预先不知道正整数的数量,一旦读到-1,就表示输入结束.然后,按照和输入相反的顺序输出所读到的数字,不包括最后标识结束的-1. 输 ...

  10. Java面试题之四

     十六.运行时异常与一般异常有何异同 Java提供了两类主要的异常:runtime exception和checked exception. 1.checked exception:这种异常也就是我们 ...