标签:stream 不能 esc seve struct 任务 空间 怎么 长度
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAX 10000 using namespace std; struct Trie { Trie *Next[94],*Fail; int no; Trie() { for(int i = 0;i < 94;i ++) { Next[i] = NULL; } Fail = NULL; no = 0; } }*root; int ans[3],c,num,vis[501]; void Insert_Str(char *s,int no) {///字符串插入到字典树中 Trie *r = root; int i = -1; while(s[++ i]) { int d = s[i] - 33; if(r -> Next[d] == NULL) { r -> Next[d] = new Trie(); } r = r -> Next[d]; } r -> no = no;///编号 } void Build_Fail() {///通过父结点的Fail更新子结点的Fail Trie *node,*temp; queue<Trie *> q; q.push(root); while(!q.empty()) { node = q.front(); q.pop(); for(int i = 0;i < 94;i ++) { if(node -> Next[i]) {///第i个儿子存在 temp = node -> Fail;///temp赋值当前节点的Fail while(temp) { if(temp -> Next[i]) { node -> Next[i] -> Fail = temp -> Next[i]; break; } temp = temp -> Fail; } if(temp == NULL) {///没找到或者本来就是根节点 node -> Next[i] -> Fail = root; } q.push(node -> Next[i]); } } } } void Ac_automation(char *s) { int i = -1; Trie *node = root,*temp; while(s[++ i]) { int d = s[i] - 33; while(node != root && node -> Next[d] == NULL) node = node -> Fail;///如果没有匹配的子结点 就找它的Fail看看有没有匹配的子结点 if(node -> Next[d]) node = node -> Next[d]; temp = node; while(temp -> no && !vis[temp -> no]) { ans[c ++] = temp -> no; vis[temp -> no] = 1; temp = temp -> Fail;///找最长后缀 } } } int main() { char s[MAX + 10]; int n,m; root = new Trie(); scanf("%d",&n); for(int i = 1;i <= n;i ++) { scanf("%s",s); Insert_Str(s,i); } Build_Fail(); scanf("%d",&m); for(int i = 1;i <= m;i ++) { scanf("%s",s); memset(vis,0,sizeof(vis)); c = 0; Ac_automation(s); if(c) { num ++; printf("web %d:",i); sort(ans,ans + c); for(int j = 0;j < c;j ++) { printf(" %d",ans[j]); } putchar(‘\n‘); } } printf("total: %d\n",num); }
但这是基于链表的,比较耗费空间和时间,时间倒不怎么明显的,基于数组的提交g++是可以过的。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAX 10000 using namespace std; int ans[10],c,num,vis[501],pos; int trie[1000010][94],fail[1000010] = {-1},no[1000010]; void Insert_Str(char *s,int k) {///字符串插入到字典树中 int i = -1,r = 0; while(s[++ i]) { int d = s[i] - 33; if(!trie[r][d]) { trie[r][d] = ++ pos; } r = trie[r][d]; } no[r] = k;///编号 } void Build_Fail() {///通过父结点的Fail更新子结点的Fail queue<int> q; q.push(0); while(!q.empty()) { int id = q.front(); q.pop(); for(int i = 0;i < 94;i ++) { if(trie[id][i]) {///第i个儿子存在 int temp = fail[id];///temp赋值当前节点的Fail while(temp != -1) { if(trie[temp][i]) { fail[trie[id][i]] = trie[temp][i]; break; } temp = fail[temp]; } q.push(trie[id][i]); } } } } void Ac_automation(char *s) { int i = -1,r = 0; while(s[++ i]) { int d = s[i] - 33; while(r && !trie[r][d]) r = fail[r];///如果没有匹配的子结点 就找它的Fail看看有没有匹配的子结点 if(trie[r][d]) r = trie[r][d]; int temp = r; while(no[temp] && !vis[no[temp]]) { ans[c ++] = no[temp]; vis[no[temp]] = 1; temp = fail[temp];///找最长后缀 } } } int main() { char s[10005]; int n,m; scanf("%d",&n); for(int i = 1;i <= n;i ++) { scanf("%s",s); Insert_Str(s,i); } Build_Fail(); scanf("%d",&m); for(int i = 1;i <= m;i ++) { scanf("%s",s); memset(vis,0,sizeof(vis)); c = 0; Ac_automation(s); if(c) { num ++; printf("web %d:",i); sort(ans,ans + c); for(int j = 0;j < c;j ++) { printf(" %d",ans[j]); } putchar(‘\n‘); } } printf("total: %d\n",num); }
标签:stream 不能 esc seve struct 任务 空间 怎么 长度
原文地址:https://www.cnblogs.com/8023spz/p/9795319.html