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. 武汉新芯:定位存储器制造,两年后或推3D NAND

    武汉新芯集成电路制造公司(XMC)2006成立,2012年底起独立经营,是国有制企业.为了区别于本土的制造巨头SMIC(中芯国际)和华力微电子(HLMC)等,XMC将立足存储器制造.近日,武汉新芯董事 ...

  2. delphi 实现vip126发邮件

    本例是 TSimpleThread , TSimpleList, IdhttpEx 网页模拟(POST)的综合运用. Demo只写了发送,但亦可收取,详见源码. (此源码写于2年前,那时还写得不好,请 ...

  3. Delphi通过IE窗口句柄获取网页接口(IWebBrowser2) good

    主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义. uses SHDoc ...

  4. 柯南君:看大数据时代下的IT架构(7)消息队列之RabbitMQ--案例(routing 起航)

    二.Routing(路由) (using the Java client) 在前面的学习中,构建了一个简单的日志记录系统,能够广播所有的日志给多个接收者,在该部分学习中,将添加一个新的特点,就是可以只 ...

  5. android加载更多的图片

    这是昨天改进后的,我测试了下,可以加载图片到5万张,估计5万以上也是没问题的,我只试到5万,其实也没必要这么高,现实中1000左右就差不多了,不过我的应用到100就差不多了, package com. ...

  6. 剑指offer 25 二叉树中和为某一值的路径

    非递归方法: class Solution { public: vector<vector<int>> FindPath(TreeNode* root,int expectNu ...

  7. 关于 FPGA 和 外部芯片接口时序设计

    在看这篇文章之前, 建议先好好读下这篇文章.http://download.csdn.net/detail/angelbosj/8013827. 因为我不太会用 VISio.要是哪位网友能告诉我.怎么 ...

  8. 如何在MFC对话框之间自定义消息传递

    在MFC项目开发中,涉及到不同模块间数据信息的传递,如用户在登录界面成功登录后向系统管理模块发送用户名和密码等信息. 首先,需明确以下两点: 谁要发送这个消息--消息发送方 谁要接受这个消息--消息接 ...

  9. sql存储过程的简单使用

    存储过程(Stored Procedure)是数据库系统中,一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它. 创建 ...

  10. My97DaePicker 用js实现文本框日期相减求天数

    <tr>                <td align="center" style="background-color: #cccccc;font ...