manacher 最长回文子串 hdu3068
2018-03-24 22:04
274 查看
hdu 3068
,因此空间复杂度是线性的。
时间复杂度:尽管代码里面有两层循环,通过amortized analysis我们可以得出
,Manacher的时间复杂度是线性的。由于内层的循环只对尚未匹配的部分进行,
因此对于每一个字符而言,只会进行一次,因此时间复杂度是O(n)。
详细解释
Manacher 算法 (马拉车)
空间复杂度:插入分隔符形成新串,占用了线性的空间大小;RL数组也占用线性大小的空间,因此空间复杂度是线性的。
时间复杂度:尽管代码里面有两层循环,通过amortized analysis我们可以得出
,Manacher的时间复杂度是线性的。由于内层的循环只对尚未匹配的部分进行,
因此对于每一个字符而言,只会进行一次,因此时间复杂度是O(n)。
详细解释
//#include <bits/stdc++.h> #include<stdio.h> #include<string.h> #include<string> #include<math.h> #include<algorithm> #include<iostream> #include<queue> #include<vector> #include<stack> #include<map> #include<set> #include<stdlib.h> #include<time.h> #include <iomanip> #define lowbit(x) (x&(-x)) #define inf 0x7fffffff #define linf 0x7fffffffffffffff #define fil(x,y) memset(x,y,sizeof(x)) #define fup(i,x,y) for(int i=(x);i<=(y);i++) #define fdn(i,x,y) for(int i=(x);i>=(y);i--) #define sp(x) setprecision(x) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define sc(n) scanf("%s",n) #define pf(x) printf("%d\n",x) #define pfl(x) printf("%lld\n",x) #define pff(x) printf("%lf\n",x) #define debug printf("!!\n"); #define N 1000005 #define M 4000009 #define pi acos(-1) #define eps 1e-2 //cout.setf(ios::fixed); //freopen("out.txt","w",stdout);// freopen("in.txt","r",stdin); using namespace std; typedef long long ll; typedef double db; char cc[110005]; char c[220005]; int a[220005]; int manacher() { int len=strlen(cc),pos=0,edge=0,maxx=0; int n=0; fup(i,0,len-1) c[n++]='$',c[n++]=cc[i]; c[n++]='$'; fup(i,0,n-1) { if(i<edge) a[i]=min(a[pos*2-i],edge-i); else a[i]=1; while(i-a[i]>=0&&i+a[i]<=n-1&&c[i-a[i]]==c[i+a[i]]) a[i]++; if(i+a[i]>edge) edge=i+a[i],pos=i; maxx=max(maxx,a[i]); } return maxx-1; } void solve() { while(~sc(cc)) { pf(manacher()); } } int main() { int t=1; // sd(t); while(t--) { solve(); } return 0; }
相关文章推荐
- 【最长回文子串】HDU3068最长回文【Manacher算法】
- [hdu3068 最长回文]Manacher算法,O(N)求最长回文子串
- HDU3068 URAL1297 Manacher求最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- hihocoder 1032 : 最长回文子串(Manacher)
- 51nod--1089 最长回文子串V2(Manacher 算法)
- hihocoder1032最长回文子串 [Manacher]
- 最长回文子串——manacher
- 最长回文子串模板---Manacher算法。时间复杂度O(N)
- 最大算法【Manacher模板】HDU 3068——求最长回文子串
- hdu 3068 最长回文(manacher&最长回文子串)
- Manacher----最长回文子串
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- Luogu-3805 (Manacher 最长回文子串)(模板)
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- hdu3068 最长回文 (最长回文子串)
- HDU 3294Girls' research最长回文子串(暴力和manacher 两种解法)
- Girls' research(已完善的Manacher算法模板:输出最长回文子串)
- Manacher算法----最长回文子串
- manacher求最长回文子串