URAL 1297
来源:互联网 发布:java 免费开源框架 编辑:程序博客网 时间:2024/06/12 23:43
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
Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)
Tags: palindromes string algorithms
)看论文。
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <queue>#include <math.h>#include <vector>using namespace std;const int maxn = 2222;int cnt[maxn],sa[maxn],height[maxn],res[maxn],rk[maxn],w[maxn];void getSa(int len,int up){ int *k=rk,*r=res,*id=height; for(int i=0;i<up;i++) cnt[i]=0; for(int i=0;i<len;i++) cnt[k[i]=w[i]]++; for(int i=0;i<up;i++) cnt[i+1]+=cnt[i]; for(int i=len-1;i>=0;i--){ sa[--cnt[k[i]]]=i; } int d=1,p=0; while(p<len){ for(int i=len-d;i<len;i++) id[p++]=i; for(int i=0;i<len;i++) if(sa[i]>=d) id[p++]=sa[i]-d; for(int i=0;i<len;i++) r[i]=k[id[i]]; for(int i=0;i<up;i++) cnt[i]=0; for(int i=0;i<len;i++) cnt[r[i]]++; for(int i=0;i<up;i++) cnt[i+1]+=cnt[i]; for(int i=len-1;i>=0;i--){ sa[--cnt[r[i]]]=id[i]; } p=0; swap(r,k); k[sa[0]]=p++; for(int i=0;i<len-1;i++){ if(sa[i]+d<len&&sa[i+1]+d<len&&r[sa[i]+d]==r[sa[i+1]+d]&&r[sa[i]]==r[sa[i+1]]){ k[sa[i+1]]=p-1; } else k[sa[i+1]]=p++; } if(p>=len) return; d=d*2,up=p,p=0; }}void getHeight(int len){ height[0]=height[1]=0;; for(int i=0;i<len;i++) rk[sa[i]]=i; for(int i=0,p=0;i<len-1;i++){ int j=sa[rk[i]-1]; while(i+p<len&&j+p<len&&w[i+p]==w[j+p]){ p++; } height[rk[i]]=p; p=max(0,p-1); }}int l;int len;void getSuffix(char s[]){ len=(int)strlen(s); l=0; for(int i=0;i<len;i++){ w[l++]=s[i]; } w[l++]=288; for(int i=len-1;i>=0;i--){ w[l++]=s[i]; } w[l++]=0; getSa(l,300); getHeight(l);}int dp[maxn][20];void prermq(){ for(int i=1;i<l;i++){ dp[i][0]=height[i]; } for(int j=1;(1<<j)<l;j++){ for(int i=1;i+(1<<j)-1<l;i++){ dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } }}int query(int ll,int rr){ if(ll>rr) swap(ll,rr); ll++; int k=(int)log2((double)(rr-ll+1)); return min(dp[ll][k],dp[rr-(1<<k)+1][k]);}int main(){ char s[maxn]; scanf("%s",s); getSuffix(s); prermq(); int ans=1; int la=0,ra=0; for(int i=0;i<l;i++){ if(i>=len) break; if(s[i]==s[i+1]){ int ll=query(rk[i+1],rk[2*len-i])-1; if(ans<2+ll+ll){ la=i-ll; ra=i+1+ll; ans=2+ll+ll; } } int ll=query(rk[i],rk[2*len-i]); if(ans<ll*2-1){ la=i-ll+1; ra=i+ll-1; ans=ll*2-1; } } for(int i=la;i<=ra;i++){ printf("%c",s[i]); } printf("\n");}
阅读全文
0 0
- URAL 1297
- ural 1297
- ural 1297
- URAL 1297
- 【回文串】ural 1297
- ural 1297 Palindrome
- Ural-1297-Palindrome
- URAL 1297 Palindrome
- URAL 1297 Palindrome
- URAL 1297Palindrome
- URAL 1297 Palindrome
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- 利用requests+re+bs4 爬取微星官网部分产品信息(主要是一些笔记本,少量台式机)
- PyQt5打开文件对话框QFileDialog
- kali安装teamviewer
- Python的生成器(generator)
- Cenos 7 搭建 hadoop
- URAL 1297
- mysql 部分函数内容小记
- SOLR对多个(关联)表创建索引
- 养成良好的习惯! 2017-09-30
- linux usb 驱动开发 抓包工具
- JavaScript数据类型的判断
- ZBrush国庆中秋大放价.....
- js编写类似c#的一串格式化字符的写法
- 等价二叉树