HDU 3065 AC自动机 模板题
来源:互联网 发布:淘宝助理添加销售属性 编辑:程序博客网 时间:2024/06/05 10:22
题目链接
水题不多说 没关freopen WA了一次
代码如下:
#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <set>#define sf scanf#define pf printfusing namespace std;const int sigma_size = 26;typedef struct Trie_Node* poi_Node;struct Trie_Node{ int v; poi_Node next[sigma_size]; poi_Node fail;};poi_Node root;poi_Node make_node(){ poi_Node cur = new Trie_Node; cur -> v = 0; memset(cur -> next,NULL,sizeof(cur -> next)); cur -> fail = NULL; return cur;}void Insert(char* s,int v){ int len = strlen(s); poi_Node cur = root; for(int i = 0;i < len;++i){ int idx = s[i] - 'A'; if(cur -> next[idx] == NULL){ cur -> next[idx] = make_node(); } cur = cur -> next[idx]; } cur -> v = v;}void get_fail(){ queue<poi_Node> q; q.push(root); while( !q.empty() ){ poi_Node cur = q.front();q.pop(); for(int i = 0;i < sigma_size;++i){ if(cur -> next[i]){ poi_Node p = cur -> fail; while(p && !p -> next[i]) p = p -> fail; cur -> next[i] -> fail = p ? p -> next[i] : root; q.push(cur -> next[i]); } } }}char str[2000000 + 5];char bug[1005][55];int cnt[1005];void Search(char* s){ int len = strlen(s); poi_Node cur = root; for(int i = 0;i < len;++i){ int idx = s[i] - 'A'; if(idx < 0 || idx >= 26){ cur = root; continue; } if(cur -> next[idx] == NULL){ while(cur && cur -> next[idx] == NULL) cur = cur -> fail; if(cur == NULL) cur = root; else cur = cur -> next[idx]; }else cur = cur -> next[idx]; poi_Node p = cur; while(p){ if(p -> v > 0){ cnt[p -> v] ++; } p = p -> fail; } }}void DFS(poi_Node ROOT){ for(int i = 0; i < sigma_size;++i) if(ROOT -> next[i]) DFS(ROOT -> next[i]); ROOT -> v = 0;}int main(){// freopen("read.txt","r",stdin); int n; root = make_node(); while( sf("%d",&n) != EOF ){ memset(cnt,0,sizeof(cnt)); for(int i = 0;i < n;++i){ sf("%s",str); strcpy(bug[i + 1],str); Insert(str,i + 1); } get_fail(); sf("%s",str); Search(str); for(int i = 1;i <= n;++i){ if(cnt[i]){ pf("%s: %d\n",bug[i],cnt[i]); } } DFS(root); } return 0;}
0 0
- HDU 3065 AC自动机模板题
- HDU 3065 AC自动机 模板题
- [HDU 3065]病毒侵袭持续中[AC自动机][模板题]
- hdu 3065 病毒侵袭持续中(ac自动机模板题)
- hdu 3065 病毒侵袭持续中 ac自动机模板题
- hdu 2896(AC自动机模板题)
- hdu 3695 AC自动机模板题
- hdu 2222 AC自动机模板题
- hdu 2222 AC自动机模板题
- hdu 2222 ac自动机模板题
- HDU 2222 ac自动机入门模板题
- HDU 2222 AC自动机模板题
- HDU - 2222 AC自动机模板题
- hdu 2222 ac自动机模板题
- hdu 2222AC自动机模板题
- HDU 2222 ac自动机模板题
- hdu 2222(AC自动机模板)
- HDU - 2222 AC 自动机模板
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解
- Android管理App的内存
- java单例模式,懒汉式和饿汉式的区别,双层锁式等
- SQLite - Java服务端
- 安装laravel
- HDU 3065 AC自动机 模板题
- 获取Assets文件值
- RedHat RHEL 安装gcc的方法
- MySQL优化必须调整的10项配置
- python——字符串与序列
- Storm在Zookeeper中的目录结构
- [2016ACM多校] HDU5787 数位DP
- 跨平台开发框架Ionic学习之路------3(使用barcodescanner扫描)
- 获取本地图片