hdu3294 Girls' research(manacher)
来源:互联网 发布:漆黑的特快列车 知乎 编辑:程序博客网 时间:2024/04/29 15:48
题目点我点我点我
题目大意:给定一个字母,把这个字母看作a,然后给一个字符串,找出最长回文串,并输出起始位置及结束位置,最后把替换字母后的最长回文串输出。
解题思路:马拉车算法裸题,注意的是,最长回文串的起始位置L=(i-p[i])/2,R=(i+p[i])/2-2。输出回文串是(s[i]-'a'-cnt+26)%26+'a'。
/* ***********************************************┆ ┏┓ ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃ ┃ ┆┆┃ ━ ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃ ┃ ┆┆┃ ┻ ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆ ┃ 勒 ┃ ┆ ┆ ┃ 戈 ┗━━━┓ ┆┆ ┃ 壁 ┣┓┆┆ ┃ 的草泥马 ┏┛┆┆ ┗┓┓┏━┳┓┏┛ ┆┆ ┃┫┫ ┃┫┫ ┆┆ ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)const int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)const int maxn = 4e5+100;int len1,len2,p[maxn];char s[maxn],str[maxn];void init(){ str[0] = '$'; str[1] = '#'; for(int i=0;i<len1;i++) { str[i*2+2] = s[i]; str[i*2+3] = '#'; } len2 = len1*2 + 2; str[len2] = '*';}void manacher(){ int id = 0,mx = 0; for(int i=0;i<len2;i++) { if(mx>i)p[i] = min(p[id*2-i],mx-i); else p[i] = 1; for(;str[i-p[i]]==str[i+p[i]];p[i]++); if(p[i]+i>mx) { mx = p[i] + i; id = i; } }}int main(){<span style="white-space:pre"></span>//freopen("in.txt","r",stdin);<span style="white-space:pre"></span>//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);char c[5];while(~scanf("%s%s",c,s)) { int cnt = c[0] - 'a'; len1 = strlen(s); init(); manacher(); int ans = 0,l,r; for(int i=0;i<len2;i++) { if(p[i]>ans) { ans = p[i]; l = (i-p[i])/2; r = (i+p[i])/2-2; } } if(ans==2)cout<<"No solution!"<<endl; else { cout<<l<<" "<<r<<endl; for(int i=l;i<=r;i++) { printf("%c",(s[i]-'a'-cnt+26)%26+'a'); } cout<<endl; } } return 0;}
0 1
- hdu3294 Girls' research(manacher)
- hdu3294 Girls' research(manacher)
- Girls' research(hdu3294+Manacher算法)
- HDU3294 Girls' research(Manacher算法)
- hdu3294 Girls' research 【manacher算法】
- HDU3294——Girls' research(manacher算法,马拉车算法)
- hdu3294 Girls' research (manacher算法,最大回文子串)
- HDU3294:Girls' research
- hdu3294---Girls' research
- HDU3294 Girls' research
- HDU3294 Girls' research
- hdu3294---Girls' research
- hdu3294-Girls' research
- hdu3294 Girls' research 回文字符串
- HDU 3294 Girls' research (manacher)
- hdoj Girls' research (字符串&manacher)
- HDOJ3294 Girls' research(Manacher)
- hdu3294(manacher)
- Android:各种Dialog创建及其监听事件实现
- CWnd类
- 258. Add Digits
- hadoop2.x HA
- 15款Cocos2d-x游戏源码
- hdu3294 Girls' research(manacher)
- 互不侵犯 [Bzoj 1087,SCOI 2005]
- 机器学习算法介绍
- Hive内置数据类型
- 无线信道平率段划分
- 摄影需要训练一双能发现不同点的眼睛
- Android图片加载选择
- 剑指offer系列-T9_4矩形覆盖
- Codeforces Round #360 (Div. 2)-B. Lovely Palindromes