hdu3294(manacher)
2016-04-15 17:22
274 查看
Girls' research
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1396 Accepted Submission(s): 532
Problem Description
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!回文串,输出要把原串处理成给定字符替代a以后,,最开始以为没有给定字符也是no solution,,wa了几次,,manacher算法本身就会计算回文串中心的位置,而且该点的len[i]也带表了回文串右侧最大长度,所以很好求了,把字符串处理一下,然后最长串长度为1输出no solution,否则根据manacher原理计算一下两边端点输出就行了
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int maxn=2000010; char str[maxn]; char tmp[maxn<<1]; int len[maxn<<1]; int init(char *st) { int i,len=strlen(st); tmp[0]='@'; for(i=1; i<=2*len; i+=2) { tmp[i]='#'; tmp[i+1]=st[i/2]; } tmp[2*len+1]='#'; tmp[2*len+2]='$'; tmp[2*len+3]=0; return 2*len+1; } int manacher(char *st,int l) { int mx=0,ans=0,po=0,flag=-1; for(int i=1; i<=l; i++) { if(mx>i) len[i]=min(mx-i,len[2*po-i]); else len[i]=1; while(st[i-len[i]]==st[i+len[i]]) len[i]++; if(len[i]+i>mx) { mx=len[i]+i; po=i; } if(ans<len[i]) { ans=len[i]; flag=i; } } return flag; } int main() { char ch[10]; while(~scanf("%s %s",ch,str)) { int l=init(str); int ans=manacher(tmp,l); int ll; if(ans%2==0) { ll=len[ans]-1; ans/=2; ans--; if(ll==1) { //cout<<"1"<<endl; printf("No solution!\n"); continue; } else { ll=(ll-1)/2; int mm=ch[0]-'a'; printf("%d %d\n",ans-ll,ans+ll); for(int i=ans-ll; i<=ans+ll; i++) { if(str[i]-mm<='z'&&str[i]-mm>='a') printf("%c",str[i]-mm); else printf("%c",str[i]-mm+26); } printf("\n"); } } else { ll=len[ans]-1; ans--; ans/=2; ans--; if(ll==1) { //cout<<"3"<<endl; printf("No solution!\n"); continue; } else { ll/=2; int mm=ch[0]-'a'; printf("%d %d\n",ans-ll+1,ans+ll); for(int i=ans-ll+1; i<=ans+ll; i++) { if(str[i]-mm<='z'&&str[i]-mm>='a') printf("%c",str[i]-mm); else printf("%c",str[i]-mm+26); } printf("\n"); } } } return 0; }
相关文章推荐
- CentOS中mysql的安装与配置
- 关于Xcode的Other Linker Flags
- 蓝桥杯 第七届省赛试题 煤球数目
- DataTable转Excel并对0开头的数值保留0显示
- 多线程应用-类(thread)
- 160415、sql语句sort排序,sort为空的在后面
- QueryRunner使用
- c3p0操作MySQL数据库
- saiku执行速度慢
- XMLHttpRequest实现终止请求和超时
- javascript 二分查找排序
- 手游性能优化之深入理解Texture Compression
- json解析,jquery解析遍历json
- PostgreSQL代码分析,查询优化部分,canonicalize_qual
- saiku执行速度慢
- ENVI软件操作【数据显示操作——Overlay菜单操作】
- Java中参数的引用传递和值传递
- 返回一个一维整数数组中最大子数组的和
- Incorrect integer value: '' for column 'RegNum' at row 1
- Android中通过EventBus传递消息数据