标签:des style color java os io strong for ar
AC自动模版题,中文题目就不叙述题意了啊。
AC自动主要是构造出字典树之后找到fail指针的跳转,类似于KMP里面的next数组的跳转啊,注意这里是多模式跳转。意思就是这个串跳到下一个串的什么位置啊。
先帖一下,做多了再一起总结吧。
3 aaa bbb ccc 2 aaabbbccc bbaacc
web 1: 1 2 3 total: 1
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> #define eps 1e-12 ///#define M 1000100 #define LL __int64 ///#define LL long long ///#define INF 0x7ffffff #define INF 0x3f3f3f3f #define PI 3.1415926535898 #define zero(x) ((fabs(x)<eps)?0:x) using namespace std; const int maxn = 1000010; struct node { int next[130], fail, flag; } f[maxn]; bool vis[510]; int Top; char s[1000010]; inline int Sel(char s) { return s-' '; } int creat() { memset(f[Top].next, -1, sizeof(f[Top].next)); f[Top].fail = -1; f[Top].flag = 0; return Top++; } void Get_tree(char *s, int root, int site, int x) { while(s[site] != '\0') { if(f[root].next[Sel(s[site])] == -1) f[root].next[Sel(s[site])] = creat(); root = f[root].next[Sel(s[site])]; site++; } f[root].flag = x; } int Get_fail(int site, int x) { while(1) { if(site == -1) return 0; if(f[site].next[x] != -1) return f[site].next[x]; site = f[site].fail; } } queue<int>q; void Get_fail(int root) { f[root].fail = -1; while(!q.empty()) q.pop(); int x; q.push(root); while(!q.empty()) { x = q.front(); q.pop(); for(int i = 0; i <= 128; i++) { if(f[x].next[i] != -1) { f[f[x].next[i]].fail = Get_fail(f[x].fail, i); q.push(f[x].next[i]); } } } } int Match(char *s, int root, int site) { int p = root; int ans = 0; int tmp; for(site = 1; s[site] != '\0'; site++) { while(p != -1 && f[p].next[Sel(s[site])] == -1) p = f[p].fail; if(p == -1) { p = root; continue; } p = f[p].next[Sel(s[site])]; tmp = p; while(tmp != root && f[tmp].flag != -1) { if(f[tmp].flag) { ans = 1; vis[f[tmp].flag] = 1; } tmp = f[tmp].fail; } } return ans; } int main() { int n; while(~scanf("%d",&n)) { Top = 0; int root = creat(); for(int i = 1; i <= n; i++) { scanf("%s", s+1); Get_tree(s, root, 1, i); } Get_fail(root); int m; scanf("%d",&m); int total = 0; int ans; for(int i = 0; i < m; i++) { memset(vis, 0, sizeof(vis)); scanf("%s",s+1); ans = Match(s, root, 1); if(ans) { total += ans; printf("web %d:", i+1); for(int j = 1; j <= n; j++) if(vis[j]) printf(" %d",j); puts(""); } } printf("total: %d\n",total); } return 0; }
标签:des style color java os io strong for ar
原文地址:http://blog.csdn.net/xu12110501127/article/details/38762237