ural 1297 最长回文子串 后缀数组
来源:互联网 发布:浙江儿童dna数据库 编辑:程序博客网 时间:2024/05/22 13:26
http://acm.timus.ru/problem.aspx?space=1&num=1297
1297. Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
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.
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
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA
给定一个字符串,求出其最长回文子串,若有长度相同的,输出最左边的。
解题思路:
后缀数组的应用,首先,将原串倒置后拼接在其后,二者之间添加一个绝对不会再字符串中出现的字符作为分隔。然后求取拼接后的字符串后缀的最长公共前缀就是答案。穷举每一位i,以i为中心的最长回文串,则只需要求出i+1和i-1倒过来的后缀最长公共前缀,或者求出i和i-1(偶数长度回文串)倒过来的最长公共前缀,所以只需要将原串反转放到原串后面然后求出height再用LCP枚举求出长度即可。
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int maxn=211111;/******************************************************************** 后缀数组 Suffix Array** INIT:solver.call_fun(char* s);** CALL: solver.lcp(int i,int j); //后缀i与后缀j的最长公共前缀** SP_USE: solver.LCS(char *s1,char* s2); //最长公共字串******************************************************************/struct SuffixArray{ int r[maxn]; int sa[maxn],rank[maxn],height[maxn]; int t[maxn],t2[maxn],c[maxn],n; int m;//模板长度 void init(char* s) { n=strlen(s); for (int i=0; i<n; i++) r[i]=int(s[i]); m=300; } int cmp(int *r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } /** 字符要先转化为正整数 待排序的字符串放在r[]数组中,从r[0]到r[n-1],长度为n,且最大值小于m。 所有的r[i]都大于0,r[n]无意义算法中置0 函数结束后,结果放在sa[]数组中(名次从1..n),从sa[1]到sa[n]。s[0]无意义 **/ void build_sa() { int i,k,p,*x=t,*y=t2; r[n++]=0; for (i=0; i<m; i++) c[i]=0; for (i=0; i<n; i++) c[x[i]=r[i]]++; for (i=1; i<m; i++) c[i]+=c[i-1]; for (i=n-1; i>=0; i--) sa[--c[x[i]]]=i; for (k=1,p=1; k<n; k*=2,m=p) { for (p=0,i=n-k; i<n; i++) y[p++]=i; for (i=0; i<n; i++) if (sa[i]>=k) y[p++]=sa[i]-k; for (i=0; i<m; i++) c[i]=0; for (i=0; i<n; i++) c[x[y[i]]]++; for (i=1; i<m; i++) c[i]+=c[i-1]; for (i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1; x[sa[0]]=0; for (i=1; i<n; i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],k)?p-1:p++; } n--; } /** height[2..n]:height[i]保存的是lcp(sa[i],sa[i-1]) rank[0..n-1]:rank[i]保存的是原串中suffix[i]的名次 **/ void getHeight() { int i,j,k=0; for (i=1; i<=n; i++) rank[sa[i]]=i; for (i=0; i<n; i++) { if (k) k--; j=sa[rank[i]-1]; while (r[i+k]==r[j+k]) k++; height[rank[i]]=k; } } int d[maxn][20]; //元素从1编号到n void RMQ_init(int A[],int n) { for (int i=1; i<=n; i++) d[i][0]=A[i]; for (int j=1; (1<<j)<=n; j++) for (int i=1; i+j-1<=n; i++) d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]); } int RMQ(int L,int R) { int k=0; L=rank[L]; R=rank[R]; if(L>R) swap(L,R); L++; while ((1<<(k+1))<=R-L+1) k++; return min(d[L][k],d[R-(1<<k)+1][k]); } void LCP_init() { RMQ_init(height,n); } int lcp(int i,int j) { if (rank[i]>rank[j]) swap(i,j); return RMQ(rank[i]+1,rank[j]); } void call_fun(char* s) { init(s);//初始化后缀数组 build_sa();//构造后缀数组sa getHeight();//计算height与rank LCP_init();//初始化RMQ }} solver;int main(){ char s[maxn],r[maxn]; while(~scanf("%s",s)) { int n=strlen(s),len=n; s[n]='#'; for(int i=n-1;i>=0;--i) s[++n]=s[i]; s[++n]='\0'; for(n=0;s[n]!='\0';++n) r[n]=s[n]; r[n]=0; solver.call_fun(r); int sum=1,id=0; for(int i=1;i<len;i++) { if(2*solver.RMQ(i+1,n-i)+1>sum) sum=2*solver.RMQ(i+1,n-i)+1,id=i-solver.RMQ(i+1,n-i); if(2*solver.RMQ(i,n-i)>sum) sum=2*solver.RMQ(i,n-i),id=i-solver.RMQ(i,n-i); } for(int i=id;i<id+sum;i++) cout<< s[i]; cout<<endl; } return 0;}/**ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA**/
0 0
- ural 1297(求最长回文子串,后缀数组)
- URAL 1297 Palindrome(最长回文子串:后缀数组)
- ural 1297 Palindrome ( 后缀数组 最长回文子串 )
- ural 1297 后缀数组求最长回文子串
- ural 1297 最长回文子串 后缀数组
- URAL 1297 后缀数组:求最长回文子串
- URAL - 1297 Palindrome(后缀数组求最长回文子串)
- 【后缀数组|最长回文子串】URAL-1297 Palindrome
- URAL 1297 最长回文子串(后缀数组)
- URAL 1297 Palindrome【后缀数组】求最长回文子串
- ural 1297. Palindrome后缀数组求最长回文子串
- URAL 1297 后缀数组+RMQ求串的最长回文子串
- ural 1297 Palindrome求最长连续回文子串(后缀数组求法)
- 【URAL】1297 Palindrome 【后缀数组+RMQ——求最长回文子串】
- 后缀数组(最长回文子串)——ural 1297
- URAL 1297 Palindrome 后缀数组 或 Manacher 求最长回文子串
- ural 1297 后缀数组求最长回文串
- URAL 1297(Palindrome,后缀数组)最长回文串
- JMX架构的简单介绍
- vc++常用函数方法
- 在Ubuntu 14.04(3.13内核)下安装Vmware Workstation 9
- 格雷码生成算法详解(c语言)
- ctypes的使用说明
- ural 1297 最长回文子串 后缀数组
- 20140805带的蜘蛛
- 李烨_敏捷团队中QA角色的转变
- Unity成立TEA联盟和3亿基金 内置国产虚拟现实
- Spring 常用标签
- 人脸识别《一》opencv人脸识别之收集人脸并学习
- C/C++ 做库文件和使用库
- Android View工作机制浅析(ppt)
- Linux下安装Tomcat服务器和部署Web应用