后缀数组可解。使用单调栈优化。
1 /* 3415 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cmath> 14 #include <ctime> 15 #include <cstring> 16 #include <climits> 17 #include <cctype> 18 #include <cassert> 19 #include <functional> 20 #include <iterator> 21 #include <iomanip> 22 using namespace std; 23 //#pragma comment(linker,"/STACK:102400000,1024000") 24 25 #define sti set<int> 26 #define stpii set<pair<int, int> > 27 #define mpii map<int,int> 28 #define vi vector<int> 29 #define pii pair<int,int> 30 #define vpii vector<pair<int,int> > 31 #define rep(i, a, n) for (int i=a;i<n;++i) 32 #define per(i, a, n) for (int i=n-1;i>=a;--i) 33 #define clr clear 34 #define pb push_back 35 #define mp make_pair 36 #define fir first 37 #define sec second 38 #define all(x) (x).begin(),(x).end() 39 #define SZ(x) ((int)(x).size()) 40 #define lson l, mid, rt<<1 41 #define rson mid+1, r, rt<<1|1 42 43 const int maxl = 1e5+5; 44 const int maxn = maxl * 2; 45 char as[maxl], bs[maxl]; 46 int a[maxn]; 47 int rank[maxn], height[maxn], sa[maxn]; 48 int wa[maxn], wb[maxn], wc[maxn], wv[maxn]; 49 int S[maxn][2]; 50 int K; 51 52 bool cmp(int *r, int a, int b, int l) { 53 return r[a]==r[b] && r[a+l]==r[b+l]; 54 } 55 56 void da(int *r, int *sa, int n, int m) { 57 int i, j, *x=wa, *y=wb, *t, p; 58 59 for (i=0; i<m; ++i) wc[i] = 0; 60 for (i=0; i<n; ++i) wc[x[i]=r[i]]++; 61 for (i=1; i<m; ++i) wc[i] += wc[i-1]; 62 for (i=n-1; i>=0; --i) sa[--wc[x[i]]] = i; 63 for (j=1,p=1; p<n; j*=2, m=p) { 64 for (p=0,i=n-j; i<n; ++i) y[p++] = i; 65 for (i=0; i<n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j; 66 for (i=0; i<n; ++i) wv[i] = x[y[i]]; 67 for (i=0; i<m; ++i) wc[i] = 0; 68 for (i=0; i<n; ++i) wc[wv[i]]++; 69 for (i=1; i<m; ++i) wc[i] += wc[i-1]; 70 for (i=n-1; i>=0; --i) sa[--wc[wv[i]]] = y[i]; 71 for (t=x,x=y,y=t, x[sa[0]]=0, p=1, i=1; i<n; ++i) 72 x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1:p++; 73 } 74 } 75 76 void calheight(int *r, int *sa, int n) { 77 int i, j, k = 0; 78 79 for (i=1; i<=n; ++i) rank[sa[i]] = i; 80 for (i=0; i<n; height[rank[i++]]=k) 81 for (k?k--:0, j=sa[rank[i]-1]; r[i+k]==r[j+k]; ++k); 82 } 83 84 void printSa(int n) { 85 for (int i=1; i<=n; ++i) 86 printf("%d ", sa[i]); 87 putchar(' '); 88 } 89 90 void printHeight(int n) { 91 for (int i=1; i<=n; ++i) 92 printf("%d ", height[i]); 93 putchar(' '); 94 } 95 96 void solve() { 97 int n = 0, nn; 98 99 for (int i=0; ; ++i) { 100 if (as[i] == '