hdoj Girls' research (字符串&manacher)
2015-12-02 19:25
344 查看
Girls' research
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1164 Accepted Submission(s): 450
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
b babd
a abcd
[align=left]Sample Output[/align]
0 2
aza
No solution!
#include<stdio.h> #include<string.h> #include<algorithm> #define N 200010 using namespace std; char s[N<<1]; char a[N<<1]; int p[N<<1]; int ans,pp; void manacher() { int m=1,r=1; ans=0;pp=0; int l=strlen(s),i; memset(p,0,sizeof(p)); p[0]=p[1]=1; for(i=2;i<l;i++) { if(r>i) p[i]=min(p[m-(i-m)],r-i); else p[i]=1; while(s[i-p[i]]==s[i+p[i]]) p[i]++; if(p[i]+i>r) { r=p[i]+i; m=i; } if(p[i]-1>ans) { ans=p[i]-1; pp=i; } } } int main() { int ll,i,j; char c[2]; while(scanf("%s%s",c,a)!=EOF) { int ll=strlen(a); s[0]='@'; for(i=0;i<ll;i++) { s[i*2+1]='#'; s[i*2+2]=a[i]; } s[ll*2+1]='#'; s[ll*2+2]='\0'; manacher(); int l=(pp-ans)/2; int r=l+ans-1; if(ans==1) printf("No solution!\n"); else { printf("%d %d\n",l,r); int t=c[0]-'a'; for(i=l;i<=r;i++) { if(a[i]-'a'>=t) printf("%c",a[i]-t); else printf("%c",a[i]-t+26); } printf("\n"); } } return 0; }
相关文章推荐
- [Android学习笔记九] Android 开发中图片灰阶(黑白)显示
- BZOJ 2165 大楼 类矩阵乘法/倍增Floyd 二进制判断状态
- RegExp 对象(正则表达式)
- Handler源码解析
- AliceBot在Idea中通过maven构建
- 2015-12-02活动目录中的组的类型和功能
- 通过ViewPager控件实现多张图片动态切换
- JAVA io流 文件流 字节流 字符流 过滤流 缓冲流
- Android中开辟线程(下载-网络连接)
- html使用心得
- Android--WebView的一些配置项
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
- Zookeeper命令行以及java API简单使用
- leetcode-54 Spiral Matrix 顺时针打印矩阵(《剑指offer》面试题20)
- IOS几种类型的动画
- 15泛型_15.10通配符
- go常见error解决办法
- 关于进程和线程的理解
- mysql的伪列
- 在PhoneGap应用中拦截CordovaWebView的url