hdoj 3065 ac自动机
来源:互联网 发布:矩阵合同 编辑:程序博客网 时间:2024/05/18 18:45
算是比较裸的ac自动机
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct cnode{ char STR[52]; int ans;}node[1010];struct trie{ trie* next; int isend; int map;//往root走到当前的结点,trie树上记录的串的映射,对应node的下标 trie* child[26]; trie() { isend = 0; map = 0; memset(child,NULL,sizeof(child)); next = NULL; }}*root,Tnum[160000];int cnt;void insert(char* str,int INDEX){ int num; int len; trie* ptr; ptr = root; int i; len = strlen(str); for(i = 0;i < len;i++) { num = str[i] - 'A'; if(ptr->child[num] == NULL) { ptr->child[num] = &Tnum[++cnt];//减少new的时间花费 } ptr = ptr->child[num]; } ptr -> isend ++; /* if(ptr->isend>1) { cout<<"yes"<<endl; }*/ ptr->map = INDEX; }void ac_automation(){ int i; queue<trie*> qu; trie* current=NULL; qu.push(root); trie* fail = NULL; while(!qu.empty()) { current = qu.front(); qu.pop(); for(i = 0;i < 26;i++) { if(current->child[i] != NULL) { fail =current->next; if(current == root) { current->child[i]->next = root; qu.push(current->child[i]); continue; } while(1) { if(fail == NULL) { current->child[i]->next = root; break; } else { if(fail->child[i] != NULL) { current->child[i]->next = fail->child[i]; break; } } fail = fail->next; } qu.push(current->child[i]); } } } }char text[2000010];int ans;void query(){ int len; len=strlen(text); trie* ptr; ptr = root; int num; int i; trie* temp; for(i = 0;i<len;i++) { if(text[i]>='A'&&text[i]<='Z') { num = text[i]-'A'; } else { ptr = root; continue; } while(ptr->child[num] ==NULL && ptr!=root) { ptr = ptr->next; } ptr = ptr->child[num]; if(ptr == NULL) { ptr = root; } temp = ptr; while(temp!=root &&temp->isend !=-1) { node[temp->map].ans+=temp->isend; // temp->isend = -1; temp = temp->next; } }}int main(){ int n; int i; char str[52]; while(scanf("%d",&n)!=EOF) { memset(Tnum,NULL,sizeof(Tnum)); root = &Tnum[0];//减少new的时间花费 cnt=0; for(i=0;i<n;i++) { scanf("%s",node[i].STR); node[i].ans =0; insert(node[i].STR,i); } getchar(); ac_automation(); // scanf("%s",text); gets(text); query(); for(i=0;i<n;i++) { if(node[i].ans != 0) { printf("%s: %d\n",node[i].STR,node[i].ans); } } } return 0;}
- hdoj 3065 ac自动机
- HDOJ-3065 AC自动机..题意大丈夫?
- 【AC自动机】 HDOJ 2243
- HDOJ 2896 AC自动机
- hdoj 3058 ac自动机+高斯消元
- 【AC自动机】 HDOJ 2296 Ring
- HDOJ 5384 Danganronpa AC自动机
- HDOJ-2222 (AC自动机模板)
- 【AC自动机】 HDOJ 3065 病毒侵袭持续中
- HDOJ-2243 AC自动机.等比矩阵求和
- HDOJ-2825 AC自动机DP+位运算..
- HDOJ-2296 AC自动机+DP..一定要细心..
- HDOJ-2896 AC自动机基本操作
- 【AC自动机】 HDOJ 2222 Keywords Search
- 【AC自动机】 HDOJ 2896 病毒侵袭
- 【AC自动机】 HDOJ 2825 Wireless Password
- 【AC自动机】 HDOJ 2457 DNA repair
- 【AC自动机】 HDOJ 3341 Lost's revenge
- VS2005中(_CRT_SECURE_NO_DEPRECATE)警告
- Android中SQLite数据库存储方式 .(转)
- 高质量C++/C试题答案
- 大明zeroson的程序员生活一周年工作总结
- 很失落
- hdoj 3065 ac自动机
- Android NDK(JNI)开发入门
- Python PyQt4实现QQ的抽屉效果
- 一个ABC眼中的中国创业环境
- 做产品和项目的区别
- jdk/bin中的native2ascii.exe使用
- seo策略,百度喜欢的seo策略
- php日期加1
- 宽带速率计算