Ural——1297Palindrome(Manacher)

20 篇文章 0 订阅
2 篇文章 0 订阅

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
 

 


模版题,但是学习到如何输出对应的回文串,即开始坐标=(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值