C - Palindrome URAL - 1297

针对U.S.Robots遭遇的间谍威胁,前雇员John Pupkin被委任拦截竞争对手Robots Unlimited的隐藏信息。任务关键在于找到文章中由间谍发布、可正反读的最长回文串。采用Manacher算法,John需处理长达1000字符的输入,忽略空格和标点,找出秘密消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C - Palindrome URAL - 1297

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.
Example
input
Kazak
output
aza

  • 题解:输出原来字符串中的回文串

  • 解题思路:直接用manacher取求回文串

#include<iostream>
#include<string>
using namespace std;
const int inf = 1e4;
string s;
char sn[inf];
int p[inf];
int main()
{	
	while(cin>>s){
		for(int i=1;i<=s.length();i++){sn[i*2-1]='$',sn[i*2]=s[i-1];}
		sn[s.length()*2+1]='$';
		int pos=0,R=0;
		int len=s.length()*2+1;
		for(int i=1;i<=len;i++){
			if(i<R)p[i]=min(p[pos*2-i],R-i);else p[i]=1;
			while(i+p[i]<=len&&i-p[i]>=1&&sn[i+p[i]]==sn[i-p[i]])p[i]++;
			if(i+p[i]>R){
				pos=i;R=i+p[i];
			}
		}
		int ans=0;pos=0;
		for(int i=1;i<=len;i++)if(ans<p[i]){ans=p[i];pos=i;}
		//ans--;int j;
		//if(ans%2){j=pos-ans/2;r=ans/2+1;}
		//else {}
		//cout<<ans-1<<endl;
			//cout<<pos<<"----"<<ans-1<<endl;
		for(int i=pos-p[pos]+1;i<pos+p[pos];i++){
			if(i%2==0)cout<<s[i/2-1];
		}
		cout<<endl;
	}
	return 0;
}

posted @ 2018-10-19 11:48 i-Curve 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值