hdu 3294 Girls' research Manacher回文串
来源:互联网 发布:淘宝office2016激活码 编辑:程序博客网 时间:2024/05/21 09:23
Girls' research(Manacher)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uAppoint description:
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.
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.
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.
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 babda abcd
Sample Output
0 2azaNo solution!
题意:给出一个加密的字符串,然后找字符串中的最长回文字串的起始位置和值
思路:先解密,再使用Manacher算法找出最长回文字串,其中有一个公式,新串中的位置( i , j )对应原始串的( (i-mp[i])/2 , (i+mp[i]-4)/2 ),很容易推导出来。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#define MAX 200010using namespace std;char ma[MAX<<1];int mp[MAX<<1];int manacher(char s[],int len){ int L=0; ma[L++]='$'; ma[L++]='#'; for(int i=0;i<len;i++) { ma[L++]=s[i]; ma[L++]='#'; } ma[L]='\0'; int mx=0,id=0; for(int i=0;i<L;i++) { mp[i]=mx>i?min(mp[2*id-i],mx-i):1; while(ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++; if(i+mp[i]>mx) { mx=i+mp[i]; id=i; } } return L;}int main(){ char s[MAX],ch[2]; int len,nlen,i,pre,maxp,maxn; int bg,ed; while(~scanf("%s%s",ch,s)) { maxp=maxn=-1; len=strlen(s); pre=ch[0]-'a'; for(i=0;i<len;i++) { s[i]=s[i]-pre; if(s[i]<'a') s[i]+=26; } nlen=manacher(s,len); for(i=1;i<nlen;i++) { if(maxn<mp[i]) { maxp=i; maxn=mp[i]; } } if(maxn-1<2) printf("No solution!\n"); else { bg=(maxp-maxn)/2;//起始位置 ed=(maxp+maxn-4)/2;//结束位置 printf("%d %d\n",bg,ed); for(i=bg;i<=ed;i++) printf("%c",s[i]); printf("\n"); } } return 0;}
0 0
- hdu 3294 Girls' research Manacher回文串
- 【回文串】HDU 3294 Girls' research manacher算法
- [manacher] hdu 3294 Girls' research
- hdu 3294 Girls' research(manacher)
- HDU - 3294 Girls' research(manacher)
- hdu 3294 Girls' research【manacher】
- HDU 3294 Girls’ research Manacher
- HDU 3294Girls' research最长回文子串(暴力和manacher 两种解法)
- HDU 3294 Girls' research(Manacher算法求回文串左右端点)
- HDU 3294 Girls' research 字符串-manacher算法
- HDU 3294 Girls' research (manacher)
- HDU.3294 Girls' research Manacher Algorithm
- hdu3294 Girls' research (manacher算法,最大回文子串)
- HDU 3294 Girls' research 【最长回文串+字符周期变换】
- HDU 3294 Girls' research【最长回文子串】
- hdoj 3294 Girls' research 【Manacher算法】【输出最长回文子串 + 字符转化】
- HDU 3294 Girls' research (Manacher算法 + 记录区间)
- HDU 3294 Girls' research (Manacher算法 + 记录区间)
- 插件大集合
- x265-1.7版本-common/frame.h注释
- 【xv6学习之番外篇】详解struct Env 与 struct Trapframe
- tomcat与nginx、Apache整合
- HDU 1098 (数学_特殊值+抽屉原理)
- hdu 3294 Girls' research Manacher回文串
- uva 10564 - Paths through the Hourglass 沙漏里的路径 最优解+路径输出
- 关于 Mac OS 本地安装 Mysql 的一些坑
- (1)指针初探
- ANR原因分析及解决方法
- boost线程同步
- python核心编程-GUI程序之标签,按钮,进度条
- 最大子矩阵
- 结构体练习