hihocoder 1032(manacher)
2017-11-09 19:46
323 查看
传送门
模板题。
P.S.差点把mx(最远点)取成min,所以以后'>','<'一定要想清楚再写。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6+4;
char a
,b[N<<1];
int p[N<<1];
inline void manacher(char *a) {
int n=strlen(a);
b[0]='$';
for (register int i=0;i<n;++i)
b[i<<1|1]='#',b[(i<<1)+2]=a[i];
b[n=n<<1|1]='#';
int id=0,mx=0,ret=0;
for (register int i=1;i<=n;++i) {
p[i]=mx>i?min(p[(id<<1)-i],mx-i):1;
while (b[i+p[i]]==b[i-p[i]]) ++p[i];
if (mx<i+p[i]) mx=i+p[i],id=i;
ret=max(ret,p[i]-1);
}
printf("%d\n",ret);
}
int main() {
int kase;
scanf("%d",&kase);
while (kase--) {
scanf("%s",a);
manacher(a);
}
return 0;
}
模板题。
P.S.差点把mx(最远点)取成min,所以以后'>','<'一定要想清楚再写。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6+4;
char a
,b[N<<1];
int p[N<<1];
inline void manacher(char *a) {
int n=strlen(a);
b[0]='$';
for (register int i=0;i<n;++i)
b[i<<1|1]='#',b[(i<<1)+2]=a[i];
b[n=n<<1|1]='#';
int id=0,mx=0,ret=0;
for (register int i=1;i<=n;++i) {
p[i]=mx>i?min(p[(id<<1)-i],mx-i):1;
while (b[i+p[i]]==b[i-p[i]]) ++p[i];
if (mx<i+p[i]) mx=i+p[i],id=i;
ret=max(ret,p[i]-1);
}
printf("%d\n",ret);
}
int main() {
int kase;
scanf("%d",&kase);
while (kase--) {
scanf("%s",a);
manacher(a);
}
return 0;
}
相关文章推荐
- hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)
- hihocoder 1032 : 最长回文子串(Manacher)
- hihocoder-1032 最长回文子串(Manacher)
- [hihoCoder 1032]最长回文子串[Manacher]
- hihoCoder-1032 - 最长回文子串(Manacher 马拉车)
- hihoCoder1032 : 最长回文子串【manacher】
- hihocoder1032:(manacher)
- hihocoder 1032 最长回文子串(Manacher)
- hihocoder1032最长回文子串 [Manacher]
- 【hihoCoder】【1032】hiho一下第一周/A:最长回文子串
- hihoCoder 1032 最长回文子串 (manacher算法)
- hihocoder 1032
- [HihoCoder]#1032 : 最长回文子串
- hihoCoder 1032 : 最长回文子串
- 【HIHOCODER 1589】回文子串的数量(Manacher)
- hdu3068 hihocoder 1032 最长回文子串 马拉车算法
- hihocoder 1032 manachar 求回文串O(n)
- hihoCoder #1032 : 最长回文子串
- hihocoder-1032
- hihocoder1032 最长回文子串