1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #include <set> #include <map> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #include <unordered_map>
#define pi acos(-1.0) #define eps 1e-9 #define fi first #define se second #define rtl rt<<1 #define rtr rt<<1|1 #define bug printf("******\n") #define mem(a, b) memset(a,b,sizeof(a)) #define name2str(x) #x #define fuck(x) cout<<#x" = "<<x<<endl #define sfi(a) scanf("%d", &a) #define sffi(a, b) scanf("%d %d", &a, &b) #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c) #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d) #define sfL(a) scanf("%lld", &a) #define sffL(a, b) scanf("%lld %lld", &a, &b) #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c) #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d) #define sfs(a) scanf("%s", a) #define sffs(a, b) scanf("%s %s", a, b) #define sfffs(a, b, c) scanf("%s %s %s", a, b, c) #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d) #define FIN freopen("../in.txt","r",stdin) #define gcd(a, b) __gcd(a,b) #define lowbit(x) x&-x #define IO iOS::sync_with_stdio(false)
using namespace std; typedef long long LL; typedef unsigned long long ULL; const ULL seed = 13331; const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; const int maxm = 8e6 + 10; const int INF = 0x3f3f3f3f; const int mod = 2012; const int maxn = 1e6 + 7;
struct Suffix_Automaton { int last, tot, nxt[maxn << 1][26], fail[maxn << 1]; int len[maxn << 1]; int sz[maxn << 1]; LL num[maxn << 1]; LL maxx[maxn << 1]; LL sum[maxn << 1]; LL subnum, sublen; int X[maxn << 1], Y[maxn << 1]; int minn[maxn << 1], mx[maxn << 1]; int L[maxn << 1];
void init() { tot = last = 1; fail[1] = len[1] = 0; for (int i = 0; i < 26; i++) nxt[1][i] = 0; }
void extend(int c) { int u = ++tot, v = last; for (int i = 0; i <= 25; i++) nxt[u][i] = 0; fail[u] = 0; L[u] = len[u] = len[v] + 1; num[u] = 1; for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u; if (!v) fail[u] = 1, sz[1]++; else if (len[nxt[v][c]] == len[v] + 1) fail[u] = nxt[v][c], sz[nxt[v][c]]++; else { int now = ++tot, cur = nxt[v][c]; len[now] = len[v] + 1; L[now] = L[cur]; memcpy(nxt[now], nxt[cur], sizeof(nxt[cur])); fail[now] = fail[cur]; fail[cur] = fail[u] = now; for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now; } last = u; }
void get_num() { for (int i = 1; i <= tot; i++) X[i] = 0; for (int i = 1; i <= tot; i++) X[len[i]]++; for (int i = 1; i <= tot; i++) X[i] += X[i - 1]; for (int i = 1; i <= tot; i++) Y[X[len[i]]--] = i; for (int i = tot; i >= 1; i--) num[fail[Y[i]]] += num[Y[i]]; }
void get_maxx(int n) { get_num(); for (int i = 1; i <= tot; i++) maxx[len[i]] = max(maxx[len[i]], num[i]); }
void get_sum() { get_num(); for (int i = tot; i >= 1; i--) { sum[Y[i]] = 1; for (int j = 0; j <= 25; j++) sum[Y[i]] += sum[nxt[Y[i]][j]]; } }
void get_subnum() { subnum = 0; for (int i = 1; i <= tot; i++) subnum += len[i] - len[fail[i]]; }
void get_sublen() { sublen = 0; for (int i = 1; i <= tot; i++) sublen += 1LL * (len[i] + len[fail[i]] + 1) * (len[i] - len[fail[i]]) / 2; }
void get_sa() { for (int i = 0; i <= tot; i++) X[i] = 0; for (int i = 1; i <= tot; i++) X[len[i]]++; for (int i = 1; i <= tot; i++) X[i] += X[i - 1]; for (int i = 1; i <= tot; i++) Y[X[len[i]]--] = i; }
void match(char s[]) { mem(mx, 0); int n = strlen(s), p = 1, maxlen = 0; for (int i = 0; i < n; i++) { int c = s[i] - 'a'; if (nxt[p][c]) p = nxt[p][c], maxlen++; else { for (; p && !nxt[p][c]; p = fail[p]); if (!p) p = 1, maxlen = 0; else maxlen = len[p] + 1, p = nxt[p][c]; } mx[p] = max(mx[p], maxlen); } for (int i = tot; i; i--) mx[fail[i]] = max(mx[fail[i]], min(len[fail[i]], mx[i])); for (int i = tot; i; i--) if (minn[i] == -1 || minn[i] > maxx[i]) minn[i] = mx[i]; }
void get_kth(int k) { int pos = 1, cnt; string s = ""; while (k) { for (int i = 0; i <= 25; i++) { if (nxt[pos][i] && k) { cnt = nxt[pos][i]; if (sum[cnt] < k) k -= sum[cnt]; else { k--; pos = cnt; s += (char) (i + 'a'); break; } } } } cout << s << endl; }
} sam;
int T, cas = 1; char s[maxn];
int main() { sfi(T); while (T--) { sfs(s); int len = strlen(s); sam.init(); printf("Case #%d:\n", cas++); for (int i = 0; i < len;) { int p, maxlen; for (p = 1, maxlen = 0; i < len;) { int c = s[i] - 'a'; if (!sam.nxt[p][c]) break; else { p = sam.nxt[p][c]; sam.extend((s[i] - 'a')); i++, maxlen++; } } if (maxlen) printf("%d %d\n", maxlen, sam.L[p] - maxlen); else printf("-1 %d\n", s[i]), sam.extend((s[i] - 'a')), i++; } }
return 0; }
|