uva10679 I Love Strings!!
2018-01-11 09:31
387 查看
题面在这里
题意:
给你一个文本串和若干模式串,问每个模式串是否在文本串中出现过。做法:
AC自动机模板题。对于重复的串的处理,可以在每个节点处挂一个链表记录模式串的编号。代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<cstdlib> #include<cctype> #include<queue> using namespace std; const int N = 1000010, M = 52000010; int n, cnt, tot; char st , s ; int ans , head[M], c [55], fail[M], vis[M]; struct Linker{ int to, nxt; Linker() {}; Linker(int x, int y) { to = x, nxt = y; }; }e ; inline int id(int x) { if(x >= 'a') return x-'a'+26; else return x-'A'; } inline void link(int x, int y) { e[++ cnt] = Linker(y, head[x]); head[x] = cnt; } inline void insert(char s[], int x) { int len = strlen(s), o = 0; for(int i = 0; i < len; i ++) { int t = id(s[i]); if(!c[o][t]) c[o][t] = ++ tot; o = c[o][t]; } link(o, x); } inline void build() { queue<int> q; for(int i = 0; i < 52; i ++) if(c[0][i]) { fail[c[0][i]] = 0; q.push(c[0][i]); } while(!q.empty()) { int u = q.front(); q.pop(); for(int i = 0; i < 52; i ++) if(c[u][i]) { fail[c[u][i]] = c[fail[u]][i]; q.push(c[u][i]); } else c[u][i] = c[fail[u]][i]; } } inline void cal(int x) { for(int i = head[x]; i; i = e[i].nxt) ans[e[i].to] = 1; } inline void query(char s[]) { int len = strlen(s), o = 0; for(int i = 0; i < len; i ++) { int t = id(s[i]); o = c[o][t]; for(int j = o; !vis[j]; j = fail[j]) cal(j), vis[j] = 1; } } int main() { int Test; scanf("%d", &Test); while(Test --) { scanf("%s%d", st, &n); tot = 0; cnt = 0; memset(head, 0, sizeof head); memset(c, 0, sizeof c); memset(fail, 0, sizeof fail); memset(ans, 0, sizeof ans); memset(vis, 0, sizeof vis); for(int i = 1; i <= n; i ++) { scanf("%s", s); insert(s, i); } build(); query(st); for(int i = 1; i <= n; i ++) puts(ans[i]?"y":"n"); } return 0; }
相关文章推荐
- UVA 10679 I love Strings!!!(AC自己主动机)
- UVA 10679 I Love Strings
- UVa 10679 - I Love Strings!!
- UVA 10679 I Love Strings!!(后缀数组)
- UVA 10679 I love Strings!!!(AC自动机)
- UVa 455 - Periodic Strings
- lightoj 1052 - String Growth & uva 12045 - Fun with Strings 矩阵
- uva 11081 Strings 字符串匹配
- Periodic Strings UVA - 455
- UVa455 Periodic Strings
- UVA - 10298 Power Strings (KMP求字符串循环节)
- 习题3-4 Periodic Strings UVa455
- UVa 10294 (Pólya计数) Arif in Dhaka (First Love Part 2)
- UVA 11081 - Strings
- UVA10298 Power Strings
- uva 11127 - Triple-Free Binary Strings(回溯)
- UVA 10294 Arif in Dhaka (First Love Part 2)(等价类)
- Periodic Strings UVA - 455 周期串WA *
- UVA 11081 Strings
- uva 455 - Periodic Strings (找字符串周期)