#include<bits/stdc++.h> #define ll long long #define ld long double #define N 2000 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m;
#include<bits/stdc++.h> #define ll long long #define ld long double #define int long long #define N 500005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m;
vector<int> KMP_match(char *S,int *F,char *T){ vector<int> ans; //回傳匹配相同地方 int p = -1; //紀錄短字串有多少被匹配 for(int i=0;T[i];i++){ //每一迴圈都讓T[i]被匹配到 while(p!=-1 && S[p+1]!=T[i]) p = F[p]; //使T[i]一定可以被匹配到 if(S[p+1] == T[i]) p += 1; //T的第i個與S的p+1可以匹配 if(!S[p+1]){ //S[p]已經匹配完成 ans.push_back(i-p); //回推匹配開頭 p = F[p]; //繼續下一輪匹配 } } return ans; }
//O(|S|)要配對的字串以及Fail Function voidKMP_build(char *S,int *F){ int p = F[0] = -1; //初始設定為-1 for(int i=1;S[i];i++){ //1到接下來字元 while(p!=-1 && S[p+1]!=S[i]) p = F[p]; //無法繼續配對,尋找更短字串 if(S[p+1] == S[i]) //配對成功(如都沒有一樣的就-1) p += 1; F[i] = p; //設定F[i] } }
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 1000005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int t,n; char S[N];
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 500005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m;
voidZ_algo(char *S,int *Z){ int l = 0,r = 0; Z[0] = 0; for(int i=1;S[i];i++){ Z[i] = max(0,min(Z[i-l],r-i)); while(S[Z[i]]&&S[Z[i]] == S[i+Z[i]]){ l = i;r = i+Z[i]; Z[i]++; } } }
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 110 #define Orz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m;
#include<bits/stdc++.h> #define ll long long #define ld long double #define int long long #define N 10005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int t,n,F[N];; char T[N],S[N];
intKMP_match(char *S,char *T,int *F){ int p = -1,ans = 0; for(int i=0;T[i];i++){ while(p!=-1 && S[p+1]!=T[i]) p = F[p]; if(S[p+1] == T[i]) p += 1; if(!S[p+1]){ ans += 1; p = F[p]; } } return ans; }
voidKMP_build(char *S,int *F){ int p = F[0] = -1; for(int i=1;S[i];i++){ while(p!=-1 && S[p+1]!=S[i]) p = F[p]; if(S[p+1] == S[i]) p += 1; F[i] = p; } }
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 1000005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m,Z[2*N]; bool ans[2*N]; char S[2*N],T[N]; //T原字串、S插入點字串
voidLongest(){ n = strlen(T);m = 2*n+1; memset(S,'.',m); for(int i=0;i<n;i++)S[2*i+1] = T[i]; Z[0] = 1; int l = 0,r = 0; for(int i=1;i<m;i++){ Z[i] = max(min(Z[2*l-i],r-i),1); while(i-Z[i]>=0 && i+Z[i] < m && S[i+Z[i]]==S[i-Z[i]]){ l = i;r = i+Z[i]; Z[i]++; } } }
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 500005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m,Z[2*N],t; bool ans[2*N]; char S[2*N],T[N]; //T原字串、S插入點字串
voidLongest(){ n = strlen(T);m = 2*n+1; memset(S,'.',m); for(int i=0;i<n;i++)S[2*i+1] = T[i]; Z[0] = 1; int l = 0,r = 0; for(int i=1;i<m;i++){ Z[i] = max(min(Z[2*l-i],r-i),1); while(i-Z[i]>=0 && i+Z[i] < m && S[i+Z[i]]==S[i-Z[i]]){ l = i;r = i+Z[i]; Z[i]++; } } }
signedmain(){ Orz; cin>>t; while(t--){ cin>>T; Longest(); int ans = 0; for(int i=0;i<m;i++){ int z = (Z[i]-1); ans = max(ans,z); } cout<<ans<<endl; } }
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 1000005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,m,Z1[3*N],Z2[3*N]; char A[3*N],B[3*N];
$O(n^2 log(n))$ is expected to score about 20-30. (Naive sorting all suffixes) $O(n log^2(n))$ is expected to score about 40. (OK for most programming contest problems) $O(n log n)$ is expected to score about 60-70. (Use counting sort for small alphabet size) $O(n)$ without tweaks is expected to score about 80-90. $O(n)$ with tweaks is expected to score 100. (This is meant for fun only :)
越後面就越毒瘤XD
test 1 - AC (score=0.000000, sig=0, time=0.009123, mem=5372) test 2 - AC (score=0.000000, sig=0, time=0.006427, mem=5508) test 3 - AC (score=0.000000, sig=0, time=0.014975, mem=5468) test 4 - AC (score=0.000000, sig=0, time=0.031332, mem=5536) test 5 - AC (score=0.000000, sig=0, time=0.026948, mem=5456) test 6 - AC (score=0.000000, sig=0, time=0.023113, mem=5396) test 7 - TLE (score=0.000000, sig=0, time=0.210000, mem=7340) test 8 - TLE (score=0.000000, sig=0, time=0.210000, mem=7152) test 9 - AC (score=0.000000, sig=0, time=0.170094, mem=7284) test 10 - AC (score=0.000000, sig=0, time=0.140098, mem=7340)
#include<bits/stdc++.h> #define ll long long #define ld long double #define int long long #define N 100005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,sa[N];
test 1 - AC (score=0.000000, sig=0, time=0.006565, mem=6048) test 2 - AC (score=0.000000, sig=0, time=0.006594, mem=6300) test 3 - AC (score=0.000000, sig=0, time=0.012431, mem=7860) test 4 - AC (score=0.000000, sig=0, time=0.012267, mem=7208) test 5 - AC (score=0.000000, sig=0, time=0.011158, mem=6804) test 6 - AC (score=0.000000, sig=0, time=0.012057, mem=7892) test 7 - AC (score=0.000000, sig=0, time=0.140876, mem=17720) test 8 - AC (score=0.000000, sig=0, time=0.077631, mem=21952) test 9 - AC (score=0.000000, sig=0, time=0.074905, mem=21340) test 10 - AC (score=0.000000, sig=0, time=0.076732, mem=30160)
#include<bits/stdc++.h> #define ll long long #define ld long double #define N 100005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,sa[N];
#include<bits/stdc++.h> #define ll long long #define ld long double #define int long long #define N 200005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,sa[N];