标签:put main script 存储 asc 不同 href nod empty
#include <iostream> #include <stdio.h> #include <cstring> #include <queue> #include <vector> #include <algorithm> using namespace std; #define Max 128 int num; int ans[5]; typedef struct TrieNode *Trie; Trie c[510]; int cn; struct TrieNode { Trie next[Max]; Trie fail; int id; int cnt; bool flag; TrieNode() { for (int i = 0; i < Max; i++) next[i] = NULL; fail = NULL; cnt = 0; flag = 0; } }; void Insert(Trie root, char *s, int id) { Trie p = root; for (int i = 0; s[i]; i++) { int t = s[i] - ‘ ‘; if (p->next[t] == NULL) { p->next[t] = new TrieNode; } p = p->next[t]; } p->cnt++; c[cn++] = p; //另记Trie的cnt; p->id = id; } void getFail(Trie root) { Trie p, now; queue<Trie> Q; Q.push(root); while (!Q.empty()) { now = Q.front(); Q.pop(); for (int i = 0; i < Max; i++) { if (now->next[i] == NULL) continue; if (now == root) now->next[i]->fail = root; else { p = now->fail; while (p) { if (p->next[i]) { now->next[i]->fail = p->next[i]; break; } p = p->fail; } if (p == NULL) now->next[i]->fail = root; } Q.push(now->next[i]); } } } int ac_automation(Trie root, char *s) { int k = 0; Trie p = root; for (int i = 0; s[i]; i++) { int t = s[i]-‘ ‘; while (!p->next[t] && p != root) p = p->fail; p = p->next[t]; if (!p) p = root; Trie temp = p; while (temp != root && temp->cnt >= 0) { if (temp->id) ans[k++] = temp->id; num += temp->cnt; temp->cnt = -1; temp = temp->fail; } } return k; } void recovery() { for (int i = 0; i < cn; i++) c[i]->cnt = 1; } int main() { //freopen("1.txt", "r", stdin); int nv, ns; char v[210], s[10010]; scanf("%d\n", &nv); Trie root = new TrieNode; for (int i = 1; i <= nv; i++) { scanf("%s", v); Insert(root, v, i); } getFail(root); scanf("%d\n", &ns); int total = 0; for (int t = 1; t <= ns; t++) { scanf("%s", s); num = 0; memset(ans, 0, sizeof(ans)); int k = ac_automation(root, s); if (num > 0) { total++; sort(ans, ans+k); printf("web %d:", t); for (int i = 0; i < k; i++) printf(" %d", ans[i]); printf("\n"); } recovery(); } printf("total: %d\n", total); return 0; }
标签:put main script 存储 asc 不同 href nod empty
原文地址:http://www.cnblogs.com/whileskies/p/7278973.html