hdu 2222 - Keywords Search
来源:互联网 发布:电脑没有网络图标 编辑:程序博客网 时间:2024/05/22 17:39
题目:给你一些单词构成的词典,统计出现过的单词个数。
分析:字符串,AC自动机。比较裸的AC自动机详细可参照本空间的:AC自动机总结。
说明:注意每个单词只统计一次。
#include <stdio.h>#include <stdlib.h>#include <string.h>/* AC_DFA define */#define nodesize 250010 //节点个数#define dictsize 26 //符集大小 typedef struct node1{ int flag; //值域 node1* fail; node1* next[dictsize];}tnode;tnode dict[nodesize+1];tnode* Q[nodesize+1];int ID[256]; class AC_DFA{ private: int size; tnode* root; public: AC_DFA() { makeID(); memset( dict, 0, sizeof( dict ) ); root=NULL; size=0; root=newnode(); } void makeID() { for ( int i = 0 ; i < 26 ; ++ i ) ID['a'+i] = i; } void init() { memset( dict, 0, sizeof( dict ) ); root=NULL; size=0; root=newnode(); } tnode* newnode() { dict[size].fail = root; return &dict[size ++]; } void insert( char* word, int l ) { tnode* now = root; for ( int i = 0 ; i < l ; ++ i ) { if ( !now->next[ID[word[i]]] ) now->next[ID[word[i]]] = newnode(); now = now->next[ID[word[i]]]; }now->flag ++; } void setfail() { Q[0] = root; root->fail = NULL; for ( int move = 0,save = 1 ; move < save ; ++ move ) { tnode* now = Q[move]; for ( int i = 0 ; i < dictsize ; ++ i ) if ( now->next[i] ) { tnode* p = now->fail; while ( p && !p->next[i] ) p = p->fail; now->next[i]->fail = p?p->next[i]:root; Q[save ++] = now->next[i]; }else now->next[i] = now==root?root:now->fail->next[i];//构建 Trie图 } } int query( char* line, int L ) {//统计字串出现个数,可重复及交叉 int sum = 0; tnode *temp,*now = root; for ( int i = 0 ; i < L ; ++ i ) { now = now->next[ID[line[i]]]; temp = now; while (temp && temp->flag) { sum += temp->flag;temp->flag = 0;temp = temp->fail;} } return sum; }};/* AC_DFA end */char Word[51];char Line[1000001];int main(){ int T,N; while ( ~scanf("%d",&T) ) for ( int t = 1 ; t <= T ; ++ t ) { AC_DFA ac; scanf("%d",&N); for ( int i = 1 ; i <= N ; ++ i ) { scanf("%s",Word); ac.insert( Word, strlen( Word ) ); } ac.setfail(); scanf("%s",Line); printf("%d\n",ac.query( Line, strlen(Line) )); } return 0;}
0 0
- hdu 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU-2222 Keywords Search
- hdu 2222 Keywords Search
- Hdu 2222 Keywords Search
- hdu 2222-Keywords Search
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- Hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU - 2222 Keywords Search
- OpenWRT的Feeds分析学习
- Centos服务器下安装配置SSL
- PC架构系列:CPU/RAM/IO总线的发展历史!
- 验证花括号成对匹配
- 安全卫士——Day06_1
- hdu 2222 - Keywords Search
- 于宙:我们这一代人的困惑
- CPU与内存的那些事
- STL之priority_queue的用法,优先队列
- 14.Hibernate的组件映射
- 单例模式一二三
- 学车之路
- hadoop 回收站
- HOJ 10444 The milliard Vasya's function(简单背包)