本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 
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!

正解:manacher

解题报告:

  manacher裸题。

  按题意所说的做就可以了,注意分奇数和偶数的情况讨论,当然也可以不讨论,弄出一个公共的式子即可。

  开始没策清楚下标关系,WA了一发...

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 400011;
int len,n,cha,p[MAXN],ans;
char ch[MAXN],s[MAXN],H;
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void manacher(){
memset(p,0,sizeof(p)); int maxR=0,id=0;
for(int i=1;i<=n;i++) {
if(i<maxR) p[i]=min(p[2*id-i],maxR-i); else p[i]=1;
for(;i+p[i]<=n && s[i-p[i]]==s[i+p[i]];p[i]++) ;
if(i+p[i]>maxR) { maxR=i+p[i]; id=i; }
}
} inline void work(){
while(scanf("%s",ch)!=EOF) {
H=ch[0]; scanf("%s",ch); len=strlen(ch); n=1;
cha=H-'a'; s[0]='%'; s[1]='#';
for(int i=0;i<len;i++) {
ch[i]-=cha; if(ch[i]<'a') ch[i]+=26;
s[++n]=ch[i];
s[++n]='#';
}
manacher(); ans=1; int pos=1;
for(int i=1;i<=n;i++) if(p[i]>ans) ans=p[i],pos=i;
ans--; if(ans==1) { printf("No solution!\n"); continue; }
int l,r,mid;
if(pos%2==0) { mid=pos/2-1; l=mid-ans/2; r=mid+ans/2; }
else { mid=pos/2-1; l=mid-ans/2+1; r=mid+ans/2; }
printf("%d %d\n",l,r);
for(int i=l;i<=r;i++) printf("%c",ch[i]);
printf("\n");
}
} int main()
{
work();
return 0;
}

  

最新文章

  1. discuz首页设置默认地址不带forum.php后缀的方法
  2. qunit学习(一)
  3. [转]NPOI 单元格级别应用
  4. mysql中间件研究(Atlas,cobar,TDDL)
  5. HTTP请求头详解【转】
  6. PHP中的魔术方法总结
  7. time.h
  8. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf3
  9. 在Mybatis-spring中由于默认Autowired导致不能配置多个数据源的问题分析及解决
  10. Android菜鸟的成长笔记(6)——剖析源码学自定义主题Theme
  11. 分布式锁2 Java非常用技术方案探讨之ZooKeeper
  12. 用JavaScript比较两个数组是否相等
  13. jQuery入门基础(选择器)
  14. 机器学习之隐马尔科夫模型HMM(六)
  15. ubuntu安装sublime-text
  16. php类之clone 克隆
  17. 【BZOJ3456】城市规划(生成函数,多项式运算)
  18. jenkins 异常
  19. 内存溢出和内存泄漏的区别(ZZ)
  20. 1657 Distance on Chessboard(简单计算题)

热门文章

  1. Android服务类Service具体解析
  2. Photoshop中磁力套索的一种简陋实现(基于Python)
  3. 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异
  4. bootstrap input 加了 disabled 后台竟然接受不到值
  5. 解决:Adb connection Error:远程主机强迫关闭了一个现有的连接
  6. CentOS 7 mini 试用笔记
  7. XSD文件详解(二)
  8. C语言基础知识【运算符】
  9. 从xhr说起
  10. Unity3d 中文菜单