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. Xampp Linux应用

    一.基本操作: 1.Xampp安装包下载:   https://www.apachefriends.org/index.html   2.安装与配置:   将xampp-linux-x64-5.6.3 ...

  2. MYSQL 用户

    MYSQL 并没有与SQL Server一样的两个级别的主体,它只有user. user 的信息都保存在mysql 数据库的 user 表中:我想也可以用insert 的方式新建用户,只是这种尝试还没 ...

  3. J2SE知识点摘记(十九)

    Collection 1.2.1         常用方法 Collection 接口用于表示任何对象或元素组.想要尽可能以常规方式处理一组元素时,就使用这一接口.Collection 在前面的大图也 ...

  4. C++ 下 typeof 的实现

    现在我们有这样一坨代码: std::vector<int> arr; // ... for(std::vector<int>::iterator iter = arr.begi ...

  5. uva 10129 poj 1386 hdu 1116 zoj 2016 play on words

    //本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...

  6. python-操作hive

    python访问hive2 HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能.目前使用python的用户可以通过pyhs2这 ...

  7. Pie(求最小身高差,dp)

    Pie Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. opencv 中cvZero()的使用

    cvZero(CvArr* ar); // 是让矩阵的值都为0,有初始化的作用, Eg:   IplImage img=cvCreateImage(cvSize(640,480),IPL_DEPTH_ ...

  9. HDOJ2031进制转换

    项目做久了,我发现自己对代码的实现能力越来越差劲了!经过前一段时间找工作的经历就能够明显感觉的到自己的代码熟练度不够!因此,今后要多加练习.要想做好一个优秀的程序猿就要多写程序,多思考. ★结题思路 ...

  10. Ceph对象存储网关中的索引工作原理<转>

    Ceph 对象存储网关允许你通过 Swift 及 S3 API 访问 Ceph .它将这些 API 请求转化为 librados 请求.Librados 是一个非常出色的对象存储(库)但是它无法高效的 ...