HDU 3294 Girls' research Manaler算法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3294 Girls' research Manaler算法相关的知识,希望对你有一定的参考价值。
题面:
One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but ‘a‘ inside is not the real ‘a‘, that means if we define the ‘b‘ is the real ‘a‘, then we can infer that ‘c‘ is the real ‘b‘, ‘d‘ is the real ‘c‘ ……, ‘a‘ is the real ‘z‘. According to this, string "abcde" changes to "bcdef".
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.
Input
Input contains multiple cases.
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real ‘a‘ is and the length of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.
Output
Please execute the operation following the two steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "No solution!".
If there are several answers available, please choose the string which first appears.
Sample Input
b babd
a abcd
Sample Output
0 2
aza
No solution!
题目大意:
现在有一个字符串,里面有一些回文串。现在让你找出其中最长的那一个,并输出。
但还有另外一个条件,每个字符串之前会有一个字符,代表着一套替换规则。
如果是b,那么就是 \(b->a,c->b,d->c...a->z\)
最后就是把替换后的字符串输出,并把这个字符串的左右界输出(字符编号从0开始)
大致思路:
如果没有替换规则就是一个manaler的裸模板。然后对于求出来的最长回文串与相对的位置进行一些操作。
这个操作分为当 \(pos\)是奇数还是偶数。
vo具体操作看代码把
//pos是定位的最长回文串的位置,len是对应的长度
if(pos%2){
l=pos-(len-1);
l/=2;
--l;
r=pos+(len-1);
r/=2;
--r;
}else{
mid=pos/2;
l=mid-len/2-1;
r=mid+len/2-1;
}
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
char str[maxn],tran[maxn<<2],basc;
int p[maxn<<2];
void print(int len)
{
for(int i=1;i<len;++i)
cout<<p[i]<<" ";
cout<<endl;
}
void Manaler()
{
memset(tran,0,sizeof(tran));
memset(p,0,sizeof(p));
int len_1=strlen(str),len_2=1;
tran[0]=‘$‘;
for(int i=0;i<len_1;++i){
tran[len_2++]=‘#‘;
tran[len_2++]=str[i];
}
tran[len_2++]=‘#‘;
tran[len_2]=‘\0‘;
int mi=0,r=0;
for(int i=1;i<len_2;++i){
if(i<=r)
p[i] = min(r-i,p[2*mi-i]);
else
p[i]=1;
while(tran[i-p[i]]==tran[i+p[i]])
++p[i];
if(i+p[i]>r){
r = i+p[i];
mi=i;
}
}
//print(len_2);
int k=basc-‘a‘,pos=0,len=0;
for(int i=1;i<len_2;++i){
if(p[i]-1>len){
len=p[i]-1;
pos=i;
}
}
if(len<2)
cout<<"No solution!"<<endl;
else{
int mid,l,r;
if(pos%2){
l=pos-(len-1);
l/=2;
--l;
r=pos+(len-1);
r/=2;
--r;
}else{
mid=pos/2;
l=mid-len/2-1;
r=mid+len/2-1;
}
cout<<l<<" "<<r<<endl;
for(int i=pos-len;i<=pos+len;++i){
if(tran[i]!=‘#‘) {
char c=(tran[i]-k);
if(c<‘a‘)
c+=26;
cout<<c;
}
}
cout<<endl;
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>basc>>str)
{
Manaler();
}
return 0;
}
以上是关于HDU 3294 Girls' research Manaler算法的主要内容,如果未能解决你的问题,请参考以下文章
HDU 3294 Girls' research Manaler算法
hdu3294Girls' research(manacher)