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
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
output
ArozaupalanalapuazorA
Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
互缀数组的经典运用,求后缀数组,这里,我们连接两个字符串,中间用一个最大的值间隔,然后,求出height,这样就可以转化成了一个后缀的最长公共前缀,枚举,中间值,分奇偶,然后,就是一个rmq问题了,这样就好办了,用一个st算法,前的复杂度是st算法n*logn+0(1)+0(n);
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define maxn 200500
int wa[maxn],wb[maxn],ws[maxn],wv[maxn],wsd[maxn],r[maxn],ans[maxn];
char str[maxn];
#define M 100010
int dp[M][18];
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int c0(int *r,int a,int b)
{return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}
int c12(int k,int *r,int a,int b)
{if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];}
void sort(int *r,int *a,int *b,int n,int m)
{int i;for(i=0;i<n;i++) wv[i]=r[a[i]];for(i=0;i<m;i++) wsd[i]=0;for(i=0;i<n;i++) wsd[wv[i]]++;for(i=1;i<m;i++) wsd[i]+=wsd[i-1];for(i=n-1;i>=0;i--) b[--wsd[wv[i]]]=a[i];return;
}
void dc3(int *r,int *sa,int n,int m)
{int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;r[n]=r[n+1]=0;for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;sort(r+2,wa,wb,tbc,m);sort(r+1,wb,wa,tbc,m);sort(r,wa,wb,tbc,m);for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;if(p<tbc) dc3(rn,san,tbc,p);else for(i=0;i<tbc;i++) san[rn[i]]=i;for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;if(n%3==1) wb[ta++]=n-1;sort(r,wb,wa,ta,m);for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;for(i=0,j=0,p=0;i<ta && j<tbc;p++)sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];for(;i<ta;p++) sa[p]=wa[i++];for(;j<tbc;p++) sa[p]=wb[j++];return;
}
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{int i,j,p,*x=wa,*y=wb,*t;for(i=0;i<m;i++) wsd[i]=0;for(i=0;i<n;i++) wsd[x[i]=r[i]]++;for(i=1;i<m;i++) wsd[i]+=wsd[i-1];for(i=n-1;i>=0;i--) sa[--wsd[x[i]]]=i;for(j=1,p=1;p<n;j*=2,m=p){for(p=0,i=n-j;i<n;i++) y[p++]=i;for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;for(i=0;i<n;i++) wv[i]=x[y[i]];//м╟ееfor(i=0;i<m;i++) wsd[i]=0;for(i=0;i<n;i++) wsd[wv[i]]++;for(i=1;i<m;i++) wsd[i]+=wsd[i-1];for(i=n-1;i>=0;i--) sa[--wsd[wv[i]]]=y[i];//м╟ееfor(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;}return;
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n){int i,j,k=0;for(i=1;i<=n;i++) rank[sa[i]]=i;for(i=0;i<n;height[rank[i++]]=k)for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);return;
}
void makermq(int n,int b[])
{int i,j;for(i=1;i<=n;i++)dp[i][0]=b[i];for(j=1;(1<<j)<=n;j++)for(i=1;i+(1<<j)-1<=n;i++)dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq(int s,int v)
{s=rank[s],v=rank[v];if(s>v)swap(s,v);s++;int k=(int)(log((v-s+1)*1.0)/log(2.0));return min(dp[s][k],dp[v-(1<<k)+1][k]);
}
int solve(int n,int l){int i,tmp,sum=0,maxx=1,last=0,j;for(i=0;i<l;i++){tmp=rmq(i,n-i-1);if(tmp*2-1>maxx){maxx=tmp*2-1;last=i-tmp+1;}tmp=rmq(i,n-i);if(tmp*2>maxx){maxx=tmp*2;last=i-tmp;}}for(i=last,j=0;j<maxx;j++)printf("%c",str[i++]);printf("\n");return maxx;
}
int main()
{int n,n1,i,tcase;while(scanf("%s",str)!=EOF){n=strlen(str);int l=n;for( i=0;i<n;i++)r[i]=str[i];r[i++]=245;for(int j=n-1;j>=0;j--){r[i++]=str[j];}r[i++]=0;n=2*n+1;int m=300;//da(r,ans,n+1,m);dc3(r,ans,n+1,m);calheight(r,ans,n);makermq(n,height);int maxx=solve(n,l);}return 0;
}

ural1297 . Palindrome相关推荐

  1. URAL1297 Palindrome【manacher算法】

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

  2. ural1297. Palindrome

    1297. Palindrome Description The "U.S. Robots" HQ has just received a rather alarming anon ...

  3. [ural1297]Palindrome

    求出后缀数组和height数组,然后将这个串反过来写在前面然后枚举中心点并求两边的最长公共前缀即可(一定要加连接符). 1 #include<bits/stdc++.h> 2 using ...

  4. 后缀数组(未完待续)

    后缀数组 简介 后缀数组(Suffix Array, SA)是一种在字符串问题中很实用的工具,其主要作用是求多模板匹配和最长公共前缀(LCP).与 AC自动机 预先处理模板串不同,后缀数组在进行多模板 ...

  5. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  6. 234. Palindrome Linked List - Easy

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  7. LeetCode 125 Valid Palindrome(有效回文)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...

  8. Lintcode108 Palindrome Partitioning || solution 题解

    [题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...

  9. 数位DP 回文序列 POJ-3280 Cheapest Palindrome

    Cheapest Palindrome [ POJ - 3280 ] 题目大意: 给定字符串s,长度为m,由n个小写字母组成.在s的任意位置增删字母,把它变成回文串,增删特定字母的花费不同,求最小花费 ...

最新文章

  1. [转]如果我有jQuery背景,我应该如何切换到AngularJS的思维模式?
  2. CV 面试问题详解宝典—目标检测篇
  3. php写简单接口_使用PHP如何编写简单的App接口
  4. 宅男程序员给老婆的计算机课程之8:控制器
  5. 使自己的注意力集中方法
  6. 韩顺平php视频笔记36 php基本语法
  7. Windows 10标题栏显示颜色
  8. mysql同时往两个表插数据_MySQL 的两个特殊数据类型属性 unsigned与 zerofill
  9. 【大数据部落】R语言电信公司churn数据客户流失 k近邻(knn)模型预测分析
  10. 【nginx】nginx的工作模式和信号量控制
  11. C语言入门经典(第5版)电子书pdf下载
  12. 原理探究——空间平滑MUSIC算法
  13. 计算机网络 自顶向下方法
  14. html游戏网站设计报告,web前端课程设计以及报告,jquery+js+css+html
  15. 四 国内IP核相关企业及其分析
  16. AAC的ADTS头解析
  17. 前端---HTML制作百度首页
  18. vue.js devtools
  19. DOS里的NET命令用法
  20. Cannot assign to read only property ‘0’ of string的报错原因之一及解决办法

热门文章

  1. 【ELT.ZIP】OpenHarmony啃论文俱乐部——人工智能短字符串压缩
  2. GO分析相关工具汇总
  3. python编写三角形_如何让代码编写python三角形?
  4. ICML 2018 | 从强化学习到生成模型:40篇值得一读的论文
  5. python高级绘图师_matplotlib库-python中的绘图师
  6. 模拟频率、数字频率、模拟角频率 之间的关系
  7. mysql 存儲emjoy表情是報錯Incorrect string value:
  8. 自定义滑条封装含有百分比例显示(swift版)
  9. 使用“不用写代码的IDE”是一种怎样的体验?
  10. twitter客户端_3个用于Linux命令行的Twitter客户端