Ural——1297Palindrome(Manacher)
来源:互联网 发布:java商城系统开发 编辑:程序博客网 时间:2024/05/14 05:08
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
模版题,但是学习到如何输出对应的回文串,即开始坐标=(id-p[id]+1)>>1。
代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<sstream>#include<cstring>#include<cstdio>#include<string>#include<deque>#include<stack>#include<cmath>#include<queue>#include<set>#include<map>#define INF 0x3f3f3f3f#define MM(x) memset(x,0,sizeof(x))using namespace std;typedef long long LL;const int N=1010;char s[N],ss[2*N];int p[2*N];inline int manacher(char s[]){int mx=p[0]=0,idd,len=strlen(s),S=0,L=0;for (int i=1; i<len; i++){p[i]=1;if(mx>i){p[i]=p[2*idd-i];if(mx-i<p[i])p[i]=mx-i;}while (s[i-p[i]]==s[i+p[i]])p[i]++;if(i+p[i]>mx){mx=i+p[i];idd=i;if(p[i]>S)S=p[i];}}return S;}int main(void){int i,j;while (~scanf("%s",s)){ss[0]='$';int len=strlen(s);for (i=0; i<len; i++){ss[2*i+1]='#';ss[2*i+2]=s[i];}ss[2*len+1]='#';int LEN=manacher(ss)-1;int idd=0;for (i=1; i<2*len+1; i++){if(p[i]>p[idd])idd=i;}int cnt=0;for (i=(idd-p[idd]+1)>>1; cnt<LEN; i++,cnt++){putchar(s[i]);}putchar('\n');MM(s);MM(ss);MM(p);}return 0;}
0 0
- Ural——1297Palindrome(Manacher)
- 【Ural 1297】Palindrome manacher 最长回文子串
- URAL 1297(后缀数组/Manacher算法)
- URAL 1297 Palindrome 后缀数组 或 Manacher 求最长回文子串
- poj3974 Palindrome(manacher)
- URAL 1297 Palindrome(后缀数组)
- URAL 1297 Palindrome (后缀数组)
- ural 1297 Palindrome
- Ural-1297-Palindrome
- URAL 1297 Palindrome
- URAL 1297 Palindrome
- URAL 1297Palindrome
- URAL 1297 Palindrome
- POJ 3974 Palindrome(manacher)
- POJ 3974 Palindrome(Manacher)
- POJ 3974 Palindrome(Manacher)
- POJ3974 Palindrome(Manacher算法)
- poj3974:Palindrome(manacher模板)
- 常见比较排序算法的实现(归并排序、快速排序、堆排序、选择排序、插入排序、希尔排序)
- 华中农业大学第四届程序设计大赛网络同步赛 D题 Robbing Red Packets
- C++对C的扩展之cin与cout
- hdoj1150(最小点覆盖)
- UVa 109 - SCUD Busters(凸包)
- Ural——1297Palindrome(Manacher)
- UVALive 6581 What does the fox say? (模拟)
- Segue属性简记
- 几种数字仿真的物理意义与代码实现(二)
- spring Transaction
- SLAM数学基础
- android studio安装和配置
- 双栈队列
- 如何创建第一个React Native android应用