1297. Palindrome

Time limit: 1.0 second
Memory limit: 64 MB
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 output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define N 2005
char a[N],bb[N];
int c[N],d[N],e[N],sa[N],height[N],n,b[N],m,dp[N][];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da()
{
int i,j,p,*x=c,*y=d,*t;
memset(b,,sizeof(b));
for(i=; i<n; i++)b[x[i]=a[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[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<n; i++)e[i]=x[y[i]];
for(i=; i<m; i++)b[i]=;
for(i=; i<n; i++)b[e[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[e[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void callheight()
{
int i,j,k=;
b[]=;
for(i=; i<n; i++)b[sa[i]]=i;
for(i=; i<n-; height[b[i++]]=k)
for(k?k--:,j=sa[b[i]-]; a[i+k]==a[j+k]; k++);
}
int fun(int i,int j)
{
i=b[i];
j=b[j];
if(i>j)swap(i,j);
i++;
int k=(int)(log(j-i+1.0)/log (2.0));
return min(dp[i][k],dp[j-(<<k)+][k]);
}
void initrmq()
{
int i,j;
memset(dp,,sizeof(dp));
for(i=; i<=n; i++)
dp[i][]=height[i];
for(j=; (<<j)<=n; j++)
for(i=; i+(<<j)<=n; i++)
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int main()
{
m=;
scanf("%s",a);
int size=n=strlen(a);
strcpy(bb,a);
a[n]='#';
strrev(bb);
strcat(a,bb);
n=strlen(a);
a[n++]='\0';
da();
callheight();
n--;
initrmq();
int maxa=,maxi=;
for(int i=; i<size; i++)
{
int tt=fun(i,n-i);
if((tt<<)>maxa)
{
maxa=tt<<;
maxi=i-tt;
}
tt=fun(i,n-i-);
if((tt<<)->maxa)
{
maxa=(tt<<)-;
maxi=i-(--tt);
}
}
for(int i=; i<maxa; i++)
putchar(a[i+maxi]);
putchar('\n');
}

1297. Palindrome ural1297(后缀数组)的更多相关文章

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

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

  2. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  3. Ural 1297 Palindrome(后缀数组+最长回文子串)

    https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...

  4. Ural1297 Palindrome(后缀数组)

        [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12406 [题意] 求最长回文子串. [思路] 将字符串 ...

  5. UVA 11475 Extend to Palindrome(后缀数组+ST表)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...

  6. [POJ3974]Palindrome(后缀数组 || manacher)

    传送门 求一个串的最长回文子串的长度 1.后缀数组 把这个串反转后接到原串的后面,中间连一个没有出现过的字符. 然后求这个新字符串的某两个后缀的公共前缀的最大值即可. ——代码 #include &l ...

  7. 【Ural1297】Palindrome(后缀数组)

    题意:求一个字符串的最长回文子串 n<=1000 思路:这是一道论文题 需要注意的细节: 1.奇偶分类 2.中间的分割符与最后的附加字母都是最小值,但两者不能相同,否则height可能会出现问题 ...

  8. ural1297 后缀数组+RMQ

    RMQ即求区间(i,j)的最值.通过O(nlogn)处理,O(1)给出答案. RMQ主要是动态规划来做.dp[i][j]表示从i开始的长为2^j的区间最值. 那么可以得到dp[i][j]=max(dp ...

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

随机推荐

  1. [2012-06-18]awk利用关联数组合并记录

    问题源起:http://bbs.chinaunix.net/thread-3753784-1-1.html 代码如下 {% capture text %} $awk '{if(!a[$1]){a[$1 ...

  2. 设置input框文字垂直居中和宽度

    input { solid #999;height:22px; background:#ffffff; line-height:22px; margin:0px; padding:0px;/*表单输入 ...

  3. 10个经典的Java面试题集合(转载)

    1.Java的HashMap是如何工作的? HashMap是一个针对数据结构的键值,每个键都会有相应的值,关键是识别这样的值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...

  4. Selenium八种基本定位方式---基于python

    from selenium import  webdriver driver=webdriver.Firefox() driver.get("https://www.baidu.com&qu ...

  5. 【转义字符】HTML 字符实体&lt; &gt: &amp;

    在开发中遇到javascript从后台获取的url 会被转义,如:http://localhost:8080/Home/Index?a=14&b=15&c=123,想把它转成http: ...

  6. RAISERROR

    RAISERROR 可以抛出一个错误,并被程序捕获,在存储过程经常使用: 是否进入Catch代码执行区域,在于错误严重等级设置 RAISERROR ('无效数据', 11 , 1) 第一个参数:自定义 ...

  7. [我所理解的REST] 2.REST用来干什么的?

    笔者每当遇到一个新事物的想去了解的时候,总是会问上自己第一个问题,这个新事物是干什么用的?在解释我所理解的REST这个过程中也不例外,这篇博客我们先关注一下REST是干什么用的,然后后续再解释REST ...

  8. 【Beta】 第六次Daily Scrum Meeting

    一.本次会议为第六次meeting会议 二.时间:10:00AM-10:20AM 地点:禹州楼 三.会议站立式照片 四.今日任务安排 成员 昨日任务 今日任务 林晓芳 对目前完成的模块进行全面测试,并 ...

  9. 四则运算题目生成程序(基于控制台)(Bug修改)

    针对上个程序中出现的bug进行修改 https://git.coding.net/cx873230936/calculator.git Bug: 1.控制台输入问题数问题 a.不能处理用户输入负数. ...

  10. 【Alpha】Daily Scrum Meeting——Day1

    站立式会议照片 每个人的工作分配 成 员 今日计划完成的任务 胡丹丹 学习手机app开发的总体流程以及搭建APP开发所需要的安卓环境,加强Java语言的学习掌握 曾丽君 学习手机app开发的总体流程以 ...