#include<algorithm> #include<cstring> #include<iostream> usingnamespace std; constint N = 1e6 + 5; int len, ans; char s[N]; structSA { int len; int sa[N], rk[N], oldrk[N], cnt[N], id[N], px[N], height[N]; boolcmp(int x, int y, int w){ return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w]; } voidbuild(char *s){ int m = 127, p; for (int i = 1; i <= len; i++) { rk[i] = s[i]; ++cnt[rk[i]]; } for (int i = 1; i <= m; i++) { cnt[i] += cnt[i - 1]; } for (int i = len; i >= 1; i--) { sa[cnt[rk[i]]--] = i; } for (int w = 1;; w *= 2, m = p) { p = 0; for (int i = len; i > len - w; i--) { id[++p] = i; } for (int i = 1; i <= len; i++) { if (sa[i] > w) { id[++p] = sa[i] - w; } } memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= len; i++) { px[i] = rk[id[i]]; ++cnt[px[i]]; } for (int i = 1; i <= m; i++) { cnt[i] += cnt[i - 1]; } for (int i = len; i >= 1; i--) { sa[cnt[px[i]]--] = id[i]; } memcpy(oldrk, rk, sizeof(rk)); p = 0; for (int i = 1; i <= len; i++) { rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p; } if (p == len) { for (int i = 1; i <= len; i++) { sa[rk[i]] = i; } break; } } } voidgetHeight(char *s){ for (int i = 1, k = 0; i <= len; ++i) { if (k) --k; int j = sa[rk[i] - 1]; while (s[i + k] == s[j + k]) ++k; height[rk[i]] = k; } } int st[N][21], lg[N]; voidinit(char *s, int n){ len = n; build(s); getHeight(s); lg[1] = 0; for (int i = 2; i <= len; ++i) lg[i] = lg[i >> 1] + 1; for (int i = 1; i <= len; ++i) { st[i][0] = height[i]; } for (int j = 0; j < 20; ++j) { for (int i = 1; i + (1 << j) <= len; ++i) { st[i][j + 1] = std::min(st[i][j], st[i + (1 << j)][j]); } } } intquery(int l, int r){ int t = lg[r - l + 1]; returnmin(st[l][t], st[r - (1 << t) + 1][t]); } intlcp(int x, int y){ if (x == y) return len - x + 1; int l = rk[x], r = rk[y]; if (l > r) swap(l, r); returnquery(l + 1, r); } } S, revS; intlcp(int x, int y){ return S.lcp(x, y); } intlcs(int x, int y){ return revS.lcp(revS.len - x + 1, revS.len - y + 1); }
voidsolve(int l, int r){ if (l >= r || r - l + 1 <= ans * 2) return; int mid = (l + r) >> 1; for (int i = mid - l + 1; i > ans; --i) if (i <= lcs(mid - i, mid) + lcp(mid - i + 1, mid + 1)) ans = max(ans, i); for (int i = r - mid; i > ans; --i) if (i <= lcs(mid, mid + i) + lcp(mid + 1, mid + i + 1)) ans = max(ans, i); solve(l, mid); solve(mid + 1, r); }
intmain(){ cin >> s + 1; len = strlen(s + 1); S.init(s, len); reverse(s + 1, s + len + 1); revS.init(s, len); solve(1, len); cout << ans * 2 << endl; return0; }
#include<cstring> #include<iostream> usingnamespace std; typedeflonglong ll; constint N = 105, M = 13; constint mod = 1e9 + 7; int n, m, c, q; int dp[2][(1 << M)][7][7]; char c1[7], c2[7]; int a1[7], a2[7], nxt1[7], nxt2[7], to1[7][4], to2[7][4]; voidKMP(int *s, int *nxt){ int j = 0; for (int i = 2; i <= c; i++) { if (j && s[j + 1] != s[i]) { j = nxt[j]; } if (s[j + 1] == s[i]) { j++; } nxt[i] = j; } } voidpre(int *s, int *nxt, int to[][4]){ for (int i = 0; i < c; i++) { for (int j = 0; j <= 2; j++) { int k = i; while (k && s[k + 1] != j) { k = nxt[k]; } if (s[k + 1] == j) { k++; } to[i][j] = k; } } } ll qpow(ll a, ll b){ ll res = 1; while (b) { if (b & 1) { res = res * a % mod; } a = a * a % mod; b /= 2; } return res; } intmain(){ cin >> n >> m >> c >> q; int U = 1 << (m - c + 1); auto trans = [](char c) -> int { if (c == 'W') return0; if (c == 'B') return1; if (c == 'X') return2; return0; }; while (q--) { cin >> c1 + 1 >> c2 + 1; for (int i = 1; i <= c; i++) { a1[i] = trans(c1[i]); a2[i] = trans(c2[i]); } KMP(a1, nxt1); KMP(a2, nxt2); pre(a1, nxt1, to1); pre(a2, nxt2, to2); memset(dp[0], 0, sizeof(dp[0])); dp[0][0][0][0] = 1; int now = 1; for (int i = 1; i <= n; i++) { memset(dp[now], 0, sizeof(dp[now])); for (int s = 0; s < U; s++) { for (int a = 0; a < c; a++) { for (int b = 0; b < c; b++) { dp[now][s][0][0] += dp[now ^ 1][s][a][b]; dp[now][s][0][0] %= mod; } } } now ^= 1; for (int j = 1; j <= m; j++) { memset(dp[now], 0, sizeof(dp[now])); for (int s = 0; s < U; s++) { for (int a = 0; a < c; a++) { for (int b = 0; b < c; b++) { if (!dp[now ^ 1][s][a][b]) continue; for (int p = 0; p <= 2; p++) { int pa = to1[a][p]; int pb = to2[b][p]; int S = s; if (j >= c) { if ((S >> (j - c)) & 1) { S ^= 1 << (j - c); } } if (pa == c) { S ^= 1 << (j - c); pa = nxt1[c]; } if (pb == c) { if ((s >> (j - c)) & 1) { continue; } pb = nxt2[c]; } dp[now][S][pa][pb] += dp[now ^ 1][s][a][b]; dp[now][S][pa][pb] %= mod; } } } } now ^= 1; } } ll ans = qpow(3, n * m); for (int i = 0; i < U; i++) { for (int j = 0; j < c; j++) { for (int k = 0; k < c; k++) { ans = (ans - dp[now ^ 1][i][j][k] + mod) % mod; } } } cout << ans << endl; } return0; } // Asusetic eru quionours