int T, n, sz; node aho[500010]; char s[1000010]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 500010; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].cnt = 0; } sz = 1; }
inlinevoidinsert(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].cnt++; }
voidbuild() { for (int i = 0; i < 26; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
intquery(char *s) { int len = strlen(s), now = 0, res = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; now = aho[now].nex[c]; for (int t = now; t && -1 != aho[t].cnt; t = aho[t].fail) { res += aho[t].cnt; aho[t].cnt = -1; } } return res; }
intmain() { scanf("%d", &T); while (T--) { init(); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", s); insert(s); } build(); scanf("%s", s); printf("%d\n", query(s)); } return0; }
node aho[N]; int n, m, sz, tot; char s[N]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < N; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].cnt = 0; } sz = 1; }
voidinsert(char *s, int id) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i]; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].cnt = id; }
voidbuild() { for (int i = 0; i < 128; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 128; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
voidquery(char *s, int id) { vector<int> res; int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i]; now = aho[now].nex[c]; for (int t = now; t && -1 != aho[t].cnt; t = aho[t].fail) { if (0 == aho[t].cnt) continue; res.push_back(aho[t].cnt); } } if (res.size()) { sort(res.begin(), res.end()); tot += 1; printf("web %d:", id); for (int i = 0; i < res.size(); i++) printf(" %d", res[i]); printf("\n"); } }
intmain() { sz = 1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", s); insert(s, i); } build(); scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%s", s); query(s, i); } printf("total: %d\n", tot); return0; }
int n, sz, res[M]; char st[M][55], s[N]; node aho[100010]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 100010; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].id = 0; } memset(res, 0, sizeof(res)); sz = 1; }
voidinsert(char *s, int id) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'A'; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].id = id; }
voidbuild() { for (int i = 0; i < 26; i++) { if (!aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
voidquery(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { if (s[i] < 'A' || s[i] > 'Z') { now = 0; continue; } int c = s[i] - 'A'; now = aho[now].nex[c]; for (int t = now; t && -1 != aho[t].id; t = aho[t].fail) { if (0 == aho[t].id) continue; res[aho[t].id]++; } } }
intmain() { while (scanf("%d", &n) != EOF) { init(); for (int i = 1; i <= n; i++) { scanf("%s", st[i]); insert(st[i], i); } build(); scanf("%s", s); query(s); for (int i = 1; i <= n; i++) { if (0 == res[i]) continue; printf("%s: %d\n", st[i], res[i]); } } return0; }
char st[200010], s[2000010]; int n, sz, mp[200010], ind[200010], res[200010]; node aho[200010]; queue<int> q;
voidinsert(char *s, int id) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; if (!aho[now].nex[c]) aho[now].nex[c] = ++sz; now = aho[now].nex[c]; } mp[id] = now; }
voidbuild() { for (int i = 0; i < 26; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); ind[0]++; } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); ind[aho[aho[u].nex[i]].fail]++; } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
voidquery(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; now = aho[now].nex[c]; aho[now].cnt++; } }
voidtopo() { for (int i = 1; i <= sz; i++) if (0 == ind[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); res[u] = aho[u].cnt; ind[aho[u].fail]--; aho[aho[u].fail].cnt += aho[u].cnt; if (0 == ind[aho[u].fail]) q.push(aho[u].fail); } }
intmain() { sz = 1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", st); insert(st, i); } build(); scanf("%s", s); query(s); topo(); for (int i = 1; i <= n; i++) printf("%d\n", res[mp[i]]); return0; }
int n, m, sz; node aho[110]; char s[15]; queue<int> q; mat mt, nuit;
voidinit() { for (int i = 0; i < sz; i++) nuit.c[i][i] = 1; }
intcalc(char c) { if ('A' == c) return0; if ('T' == c) return1; if ('C' == c) return2; return3; }
voidinsert(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = calc(s[i]); if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].mk = 1; }
voidbuild() { for (int i = 0; i < 4; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (aho[aho[u].fail].mk) aho[u].mk = 1; for (int i = 0; i < 4; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } for (int i = 0; i < sz; i++) for (int k = 0; k < 4; k++) if (!aho[i].mk && !aho[aho[i].nex[k]].mk) mt.c[i][aho[i].nex[k]]++; }
mat mul(mat a, mat b) { mat res; for (int i = 0; i < sz; i++) { for (int k = 0; k < sz; k++) { res.c[i][k] = 0; for (int t = 0; t < sz; t++) res.c[i][k] += a.c[i][t] * b.c[t][k]; res.c[i][k] %= mod; } } return res; }
mat power(mat a, int n) { init(); mat res = nuit; while (n) { if (n & 1) res = mul(res, a); a = mul(a, a); n >>= 1; } return res; }
intmain() { sz = 1; scanf("%d%d", &m, &n); while (m--) { scanf("%s", s); insert(s); } build(); mat res = power(mt, n); ll sum = 0; for (int i = 0; i < sz; i++) sum += res.c[0][i]; printf("%lld\n", sum % mod); return0; }
node aho[40]; int n, m, sz, l; char s[10]; queue<int> q; mat mt, nuit;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 40; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].mk = 0; } sz = 1; }
voidinsert(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].mk = 1; }
voidbuild() { for (int i = 0; i < 26; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (aho[aho[u].fail].mk) aho[u].mk = 1; for (int i = 0; i < 26; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } memset(mt.c, 0, sizeof(mt.c)); for (int i = 0; i < sz; i++) for (int k = 0; k < 26; k++) if (!aho[i].mk && !aho[aho[i].nex[k]].mk) mt.c[i][aho[i].nex[k]]++; for (int i = 0; i <= sz; i++) mt.c[i][sz] = 1; }
mat mul(mat a, mat b) { mat res; for (int i = 0; i <= l; i++) { for (int k = 0; k <= l; k++) { res.c[i][k] = 0; for (int t = 0; t <= l; t++) res.c[i][k] += a.c[i][t] * b.c[t][k]; } } return res; }
mat power(mat a, int n) { for (int i = 0; i <= l; i++) nuit.c[i][i] = 1; mat res = nuit; while (n) { if (n & 1) res = mul(res, a); a = mul(a, a); n >>= 1; } return res; }
intmain() { while (scanf("%d%d", &n, &m) != EOF) { init(); for (int i = 1; i <= n; i++) { scanf("%s", s); insert(s); } build(); l = sz; mat ans = power(mt, m); ull res = 0; for (int i = 0; i <= sz; i++) res += ans.c[0][i]; l = 1; mt.c[0][0] = 26; mt.c[0][1] = 0; mt.c[1][0] = mt.c[1][1] = 1; ans = power(mt, m); printf("%llu\n", ans.c[1][0] + ans.c[0][0] - res); } return0; }
structBigInt { conststaticint mod = 10000; conststaticint dlen = 4; int a[100], len; BigInt() { memset(a, 0, sizeof(a)); len = 1; } BigInt(int v) { memset(a, 0, sizeof(a)); len = 0; do { a[len++] = v % mod; v /= mod; } while (v); } BigInt(constchar *s) { memset(a, 0, sizeof(a)); int L = strlen(s), id = 0; len = L / dlen; if (L % dlen) len++; for (int i = L - 1; i >= 0; i -= dlen) { int t = 0, k = i - dlen + 1; if (k < 0) k = 0; for (int j = k; j <= i; j++) t = t * 10 + s[j] - '0'; a[id++] = t; } } BigInt operator + (const BigInt &b) const { BigInt res; res.len = max(len, b.len); for (int i = 0; i <= res.len; i++) res.a[i] = 0; for (int i = 0; i < res.len; i++) { res.a[i] += ((i < len) ? a[i] : 0) + ((i < b.len) ? b.a[i] : 0); res.a[i + 1] += res.a[i] / mod; res.a[i] %= mod; } if (res.a[res.len] > 0) res.len++; return res; } BigInt operator * (const BigInt &b) const { BigInt res; for (int i = 0; i < len; i++) { int up = 0; for (int j = 0; j < b.len; j++) { int t = a[i] * b.a[j] + res.a[i + j] + up; res.a[i + j] = t % mod; up = t / mod; } if (0 != up) res.a[i + b.len] = up; } res.len = len + b.len; while (res.a[res.len - 1] == 0 && res.len > 1) res.len--; return res; } voidoutput(){ printf("%d", a[len - 1]); for (int i = len - 2; i >= 0; i--) printf("%04d", a[i]); printf("\n"); } };
int n, m, p, sz; node aho[110]; map<char, int> mp; char s[55]; queue<int> q; BigInt dp[55][110];
voidinsert(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = mp[s[i]]; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].mk = 1; }
voidbuild() { for (int i = 0; i < n; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (aho[aho[u].fail].mk) aho[u].mk = 1; for (int i = 0; i < n; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
intmain() { sz = 1; scanf("%d%d%d", &n, &m, &p); scanf("%s", s); for (int i = 0; i < n; i++) mp[s[i]] = i; for (int i = 1; i <= p; i++) { scanf("%s", s); insert(s); } build(); for (int i = 0; i <= m; i++) for (int k = 0; k < sz; k++) dp[i][k] = 0; dp[0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j < sz; j++) { if (aho[j].mk) continue; for (int k = 0; k < n; k++) { int v = aho[j].nex[k]; if (aho[v].mk) continue; dp[i + 1][v] = dp[i + 1][v] + dp[i][j]; } } } BigInt res = 0; for (int i = 0; i < sz; i++) res = res + dp[m][i]; res.output(); return0; }
node aho[110]; int n, m, k, sz; int dp[27][110][1 << 10], bc[1 << 10]; char s[15]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 110; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].v = 0; } sz = 1; }
voidinsert(char *s, int v) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].v |= v; }
voidbuild() { for (int i = 0; i < 26; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); aho[u].v |= aho[aho[u].fail].v; for (int i = 0; i < 26; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
intmain() { bc[0] = 0; for (int t = 1; t < (1 << 10); t++) bc[t] = bc[t >> 1] + (t & 1); while (scanf("%d%d%d", &n, &m, &k) && 0 != (n + m + k)) { init(); for (int i = 0; i < m; i++) { scanf("%s", s); insert(s, 1 << i); } build(); for (int i = 0; i <= n; i++) for (int j = 0; j < sz; j++) for (int t = 0; t < (1 << m); t++) dp[i][j][t] = 0; dp[0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < sz; j++) { for (int t = 0; t < (1 << m); t++) { if (0 == dp[i][j][t]) continue; for (int p = 0; p < 26; p++) { int pt = aho[j].nex[p]; dp[i + 1][pt][t | aho[pt].v] += dp[i][j][t]; dp[i + 1][pt][t | aho[pt].v] %= mod; } } } } int res = 0; for (int t = 0; t < (1 << m); t++) { if (bc[t] < k) continue; for (int i = 0; i < sz; i++) res = (res + dp[n][i][t]) % mod; } printf("%d\n", res); } return0; }
int T, n, m, sz, dp[55][1110]; node aho[1110]; string p[55][1110]; char s[110][20]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 1110; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].val = 0; } sz = 1; }
voidinsert(char *s, int v) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - 'a'; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].val += v; }
voidbuild() { for (int i = 0; i < 26; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); aho[u].val += aho[aho[u].fail].val; for (int i = 0; i < 26; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
inline string comp(string a, string b) { if ("" == a) return b; if (a.size() != b.size()) return a.size() < b.size() ? a : b; return a < b ? a : b; }
voidsolve() { for (int i = 0; i <= n; i++) { for (int k = 0; k < sz; k++) { dp[i][k] = -1; p[i][k] = ""; } } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int k = 0; k < sz; k++) { if (-1 == dp[i][k]) continue; for (int t = 0; t < 26; t++) { int v = aho[k].nex[t]; if (dp[i + 1][v] > dp[i][k] + aho[v].val) continue; if (dp[i + 1][v] < dp[i][k] + aho[v].val) { dp[i + 1][v] = dp[i][k] + aho[v].val; p[i + 1][v] = p[i][k] + char(t + 'a'); } else { p[i + 1][v] = comp(p[i + 1][v], p[i][k] + char(t + 'a')); } } } } int imax = 0; string res = ""; for (int i = 0; i <= n; i++) { for (int k = 0; k < sz; k++) { if (dp[i][k] > imax) { imax = dp[i][k]; res = p[i][k]; } elseif (dp[i][k] == imax) { res = comp(res, p[i][k]); } } } if (0 == imax) printf("\n"); else cout << res << endl; }
intmain() { scanf("%d", &T); while (T--) { init(); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%s", s[i]); for (int i = 1; i <= m; i++) { int val; scanf("%d", &val); insert(s[i], val); } build(); solve(); } return0; }
int n, sz, dp[1010][1010], icas; node aho[1010]; char st[25], s[1010]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 1010; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].mk = 0; } sz = 1; }
intcalc(char c) { if ('A' == c) return0; if ('G' == c) return1; if ('C' == c) return2; return3; }
voidinsert(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = calc(s[i]); if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].mk = 1; }
voidbuild() { for (int i = 0; i < 4; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (aho[aho[u].fail].mk) aho[u].mk = 1; for (int i = 0; i < 4; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
intquery(char *s) { int len = strlen(s + 1); for (int i = 1; i <= len; i++) for (int k = 0; k < sz; k++) dp[i][k] = INF; for (int i = 0; i < len; i++) { for (int k = 0; k < sz; k++) { if (INF == dp[i][k]) continue; if (aho[k].mk) continue; for (int t = 0; t < 4; t++) { int v = aho[k].nex[t], c = calc(s[i + 1]); if (aho[v].mk) continue; if (t == c) dp[i + 1][v] = min(dp[i + 1][v], dp[i][k]); else dp[i + 1][v] = min(dp[i + 1][v], dp[i][k] + 1); } } } int imin = INF; for (int i = 0; i < sz; i++) imin = min(imin, dp[len][i]); return INF == imin ? -1 : imin; }
intmain() { while (scanf("%d", &n) && 0 != n) { init(); for (int i = 1; i <= n; i++) { scanf("%s", st); insert(st); } build(); scanf("%s", s + 1); printf("Case %d: %d\n", ++icas, query(s)); } return0; }
node aho[510]; int n, sz, f[510][15010], h[45][45][45][45]; int ct[4], icas; queue<int> q; char st[15], s[45];
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 510; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].cnt = 0; } sz = 1; }
intcalc(char c) { if ('A' == c) return0; if ('C' == c) return1; if ('G' == c) return2; return3; }
voidinsert(char *s) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = calc(s[i]); if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].cnt++; }
voidbuild() { for (int i = 0; i < 4; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); aho[u].cnt += aho[aho[u].fail].cnt; for (int i = 0; i < 4; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
intquery(char *s) { memset(ct, 0, sizeof(ct)); int len = strlen(s), cnt = 0; for (int i = 0; i < len; i++) ct[calc(s[i])]++; for (int a = 0; a <= ct[0]; a++) for (int b = 0; b <= ct[1]; b++) for (int c = 0; c <= ct[2]; c++) for (int d = 0; d <= ct[3]; d++) h[a][b][c][d] = cnt++; for (int i = 0; i < sz; i++) for (int k = 0; k < cnt; k++) f[i][k] = -1; f[0][0] = 0; for (int a = 0; a <= ct[0]; a++) { for (int b = 0; b <= ct[1]; b++) { for (int c = 0; c <= ct[2]; c++) { for (int d = 0; d <= ct[3]; d++) { for (int i = 0; i < sz; i++) { int hu = h[a][b][c][d]; if (-1 == f[i][hu]) continue; if (a < ct[0]) { int v = aho[i].nex[0]; int hv = h[a + 1][b][c][d]; f[v][hv] = max(f[v][hv], f[i][hu] + aho[v].cnt); } if (b < ct[1]) { int v = aho[i].nex[1]; int hv = h[a][b + 1][c][d]; f[v][hv] = max(f[v][hv], f[i][hu] + aho[v].cnt); } if (c < ct[2]) { int v = aho[i].nex[2]; int hv = h[a][b][c + 1][d]; f[v][hv] = max(f[v][hv], f[i][hu] + aho[v].cnt); } if (d < ct[3]) { int v = aho[i].nex[3]; int hv = h[a][b][c][d + 1]; f[v][hv] = max(f[v][hv], f[i][hu] + aho[v].cnt); } } } } } } int hm = h[ct[0]][ct[1]][ct[2]][ct[3]], imax = 0; for (int i = 0; i < sz; i++) imax = max(imax, f[i][hm]); return imax; }
intmain() { while (scanf("%d", &n) && 0 != n) { init(); for (int i = 1; i <= n; i++) { scanf("%s", st); insert(st); } build(); scanf("%s", s); printf("Case %d: %d\n", ++icas, query(s)); } return0; }
int n, m, sz, pos[15], d[15][15]; int dis[60010], dp[1100][15]; node aho[60010]; char s[50010]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 60010; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].v = 0; } sz = 1; }
voidinsert(char *s, int v) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = s[i] - '0'; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].v = v; }
voidbuild() { for (int i = 0; i < 2; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (-1 == aho[aho[u].fail].v) aho[u].v = -1; else aho[u].v |= aho[aho[u].fail].v; for (int i = 0; i < 2; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
voidbfs(int s, int sum) { for (int i = 0; i < sz; i++) dis[i] = -1; queue<int> sq; sq.push(pos[s]); dis[pos[s]] = 0; while (!sq.empty()) { int u = sq.front(); sq.pop(); for (int i = 0; i < 2; i++) { int v = aho[u].nex[i]; if (-1 == dis[v] && -1 != aho[v].v) { dis[v] = dis[u] + 1; sq.push(v); } } } for (int i = 0; i <= sum; i++) d[s][i] = dis[pos[i]]; }
intmain() { while (scanf("%d%d", &n, &m) && 0 != (n + m)) { init(); for (int i = 0; i < n; i++) { scanf("%s", s); insert(s, 1 << i); } for (int i = 0; i < m; i++) { scanf("%s", s); insert(s, -1); } build(); int sum = 0; for (int i = 0; i < sz; i++) if (aho[i].v > 0) pos[++sum] = i; bfs(0, sum); for (int i = 1; i <= sum; i++) bfs(i, sum); memset(dp, INF, sizeof(dp)); dp[0][0] = 0; for (int i = 0; i < (1 << n); i++) { for (int k = 0; k <= sum; k++) { if (INF == dp[i][k]) continue; for (int t = 0; t <= sum; t++) { if (k == t || -1 == d[k][t]) continue; int nt = pos[t], v = i | aho[nt].v; dp[v][t] = min(dp[v][t], dp[i][k] + d[k][t]); } } } int res = INF; for (int i = 1; i <= sum; i++) res = min(res, dp[(1 << n) - 1][i]); printf("%d\n", res); } return0; }
node aho[210]; int T, m, n, sz, f[210][110][110][4]; char s[110]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 210; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].v = 0; } sz = 1; }
inlineintcalc(char c) { if ('R' == c) return0; return1; }
voidinsert(char *s, int v) { int len = strlen(s), now = 0; for (int i = 0; i < len; i++) { int c = calc(s[i]); if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].v |= v; }
voidbuild() { for (int i = 0; i < 2; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); aho[u].v |= aho[aho[u].fail].v; for (int i = 0; i < 2; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
intquery() { for (int i = 0; i < sz; i++) for (int a = 0; a <= m; a++) for (int b = 0; b <= n; b++) for (int t = 0; t < 4; t++) f[i][a][b][t] = 0; f[0][0][0][0] = 1; for (int a = 0; a <= m; a++) { for (int b = 0; b <= n; b++) { for (int i = 0; i < sz; i++) { for (int t = 0; t < 4; t++) { if (0 == f[i][a][b][t]) continue; int &val = f[i][a][b][t]; if (a < m) { int v = aho[i].nex[0]; f[v][a + 1][b][t | aho[v].v] += val; f[v][a + 1][b][t | aho[v].v] %= mod; } if (b < n) { int v = aho[i].nex[1]; f[v][a][b + 1][t | aho[v].v] += val; f[v][a][b + 1][t | aho[v].v] %= mod; } } } } } int res = 0; for (int i = 0; i < sz; i++) res = (res + f[i][m][n][3]) % mod; return res; }
intmain() { scanf("%d", &T); while (T--) { init(); scanf("%d%d", &m, &n); for (int i = 0; i < 2; i++) { scanf("%s", s); insert(s, 1 << i); } build(); printf("%d\n", query()); } return0; }
node aho[510]; int n, m, sz, s[10]; double f[55][510]; point p[55]; queue<int> q;
voidinit() { while (!q.empty()) q.pop(); for (int i = 0; i < 510; i++) { memset(aho[i].nex, 0, sizeof(aho[i].nex)); aho[i].fail = aho[i].mk = 0; } sz = 1; }
voidinsert(int len) { int now = 0; for (int i = 1; i <= len; i++) { int c = s[i]; if (!aho[now].nex[c]) aho[now].nex[c] = sz++; now = aho[now].nex[c]; } aho[now].mk = 1; }
voidbuild() { for (int i = 1; i <= n; i++) { if (0 == aho[0].nex[i]) continue; aho[aho[0].nex[i]].fail = 0; q.push(aho[0].nex[i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (aho[aho[u].fail].mk) aho[u].mk = 1; for (int i = 1; i <= n; i++) { if (aho[u].nex[i]) { aho[aho[u].nex[i]].fail = aho[aho[u].fail].nex[i]; q.push(aho[u].nex[i]); } else aho[u].nex[i] = aho[aho[u].fail].nex[i]; } } }
doubledis(point a, point b) { double dx = a.x - b.x; double dy = a.y - b.y; double res = dx * dx + dy * dy; returnsqrt(res); }
doublesolve() { for (int i = 1; i <= n; i++) for (int k = 0; k < sz; k++) f[i][k] = INF; f[1][aho[0].nex[1]] = 0; for (int i = 1; i < n; i++) { for (int k = 0; k < sz; k++) { if (f[i][k] == INF) continue; for (int t = i + 1; t <= n; t++) { int v = aho[k].nex[t]; if (aho[v].mk) continue; f[t][v] = min(f[t][v], f[i][k] + dis(p[i], p[t])); } } } double res = INF; for (int i = 0; i < sz; i++) { if (INF == f[n][i]) continue; res = min(res, f[n][i]); } return res; }
intmain() { while (scanf("%d%d", &n, &m) && 0 != (n + m)) { init(); for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); for (int i = 1; i <= m; i++) { int k; scanf("%d", &k); for (int t = 1; t <= k; t++) scanf("%d", &s[t]); insert(k); } build(); double res = solve(); if (INF == res) printf("Can not be reached!\n"); elseprintf("%.2lf\n", res); } return0; }