标签:include printf init scanf 多少 mes rmq abc 最大
#include<bits/stdc++.h> using namespace std; const int N = 3e5; const int MAX_CHAR = 255;//每个数字的最大值。 char s[N + 10];//如果是数字,就写成int s[N+10]就好,从0开始存 int Sa[N + 10], T1[N + 10], T2[N + 10], C[N+10]; int Height[N + 10], Rank[N + 10]; void build_Sa(int n, int m) { int i, *x = T1, *y = T2; for (i = 0; i<m; i++) C[i] = 0; for (i = 0; i<n; i++) C[x[i] = s[i]]++; for (i = 1; i<m; i++) C[i] += C[i - 1]; for (i = n - 1; i >= 0; i--) Sa[--C[x[i]]] = i; for (int k = 1; k <= n; k <<= 1) { int p = 0; for (i = n - k; i<n; i++) y[p++] = i; for (i = 0; i<n; i++) if (Sa[i] >= k) y[p++] = Sa[i] - k; for (i = 0; i<m; i++) C[i] = 0; for (i = 0; i<n; i++) C[x[y[i]]]++; for (i = 1; i<m; i++) C[i] += C[i - 1]; for (i = n - 1; i >= 0; i--) Sa[--C[x[y[i]]]] = y[i]; swap(x, y); p = 1; x[Sa[0]] = 0; for (i = 1; i<n; i++) x[Sa[i]] = y[Sa[i - 1]] == y[Sa[i]] && y[Sa[i - 1] + k] == y[Sa[i] + k] ? p - 1 : p++; if (p >= n) break; m = p; } } void getHeight(int n) { int i, j, k = 0; for (i = 1; i <= n; i++) Rank[Sa[i]] = i; for (i = 0; i<n; i++) { if (k) k--; j = Sa[Rank[i] - 1]; while (s[i + k] == s[j + k]) k++; Height[Rank[i]] = k; } } const int MAXL = 19;//log2数组的最大长度 const int INF = 0x3f3f3f3f;//数值绝对值的最大值 struct abc{ int pre2[MAXL+5],need[N+10]; int fmin[N+10][MAXL+5]; void init(int n) { pre2[0] = 1; for (int i = 1;i <= MAXL;i++) { pre2[i] = pre2[i-1]<<1; } need[1] = 0; need[2] = 1; int temp = 2; for (int i = 3; i <= n; i++)//need[i]表示长度为i是2的多少次方,可以理解为[log2i] if (pre2[temp] == i) need[i] = need[i - 1] + 1, temp++; else need[i] = need[i - 1]; } void getst(int *a,int n) { memset(fmin,INF,sizeof fmin); for (int i = 1;i <= n;i++)//下标从0开始就改成对应的就好 fmin[i][0] = a[i]; for (int l = 1;pre2[l]<=n;l++) for (int i = 1;i <= n;i++) if (i+pre2[l]-1<=n) fmin[i][l] = min(fmin[i][l-1],fmin[i+pre2[l-1]][l-1]); } int getmin(int l,int r) { int len = need[r-l+1]; return min(fmin[l][len],fmin[r-pre2[len]+1][len]); } }ST; char S[2*N+100]; int belong[2*N+100],p[2*N+100]; int n; long long ans = 0; int lcp(int x,int y) { return ST.getmin(x+1,y); } void cal(int x,int y) { int len = y - x + 1;//子串s[x..y]的长度 int temp = Rank[x];//这个后缀x的排名 int pr = temp;//最右能到哪里 int l = temp+1,r = n; while (l <= r) { int mid = (l+r)>>1; if (lcp(temp,mid)>=len) { pr = mid; l = mid + 1; }else r = mid - 1; } int pl = temp; l = 1,r = temp - 1; while (l <= r) { int mid = (l+r)>>1; if (lcp(mid,temp)>=len) { pl = mid; r = mid - 1; }else l = mid + 1; } ans = max(ans,1LL*(pr-pl+1)*len); } int main() { //freopen("F:\\rush.txt","r",stdin); scanf("%s", s); n = strlen(s); s[n] = 0; build_Sa(n + 1, MAX_CHAR);//注意调用n+1 getHeight(n); ST.init(n); ST.getst(Height,n); int len = 0; S[len++] = ‘$‘; S[len++] = ‘#‘; for (int i = 0;i < n;i++) { S[len++] = s[i]; belong[len-1] = i; S[len++] = ‘#‘; } int mx = 0,id = 0; for (int i = 1;i < len;i++) { if (i > mx) p[i] = 1; else p[i] = min(p[2*id-i],mx-i+1); while (S[i-p[i]]==S[i+p[i]]) { if (i+p[i] > mx && S[i-p[i]+1]!=‘#‘) { cal(belong[i-p[i]+1],belong[i+p[i]-1]); } p[i]++; } if (i + p[i]-1 > mx) mx = i + p[i] - 1,id = i; } printf("%lld\n",ans); return 0; }
标签:include printf init scanf 多少 mes rmq abc 最大
原文地址:http://www.cnblogs.com/AWCXV/p/7631373.html