#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;#define NUM 26
#define ST 'a'
#define QMAX 500001
struct Node
{Node *fail;Node *nxt[NUM];int count; // 单词结束点+1Node(){fail = NULL;memset( nxt,0,sizeof(nxt) );count = 0;}
}*Q[QMAX];int head, tail;
char text[5000001]; //文本
void insert( char *str, Node *root )
{int len = strlen( str );Node *temp = root;for( int i = 0;i < len;i++ ){int nxtp = str[i] - ST;if( !temp->nxt[ nxtp ] ){temp->nxt[ nxtp ] = new Node();}temp = temp->nxt[ nxtp ];}temp->count++;
}void Build_AC( Node *root )
{root->fail = NULL;Q[ head++ ] = root;if( head == QMAX ) head = 0;while( head != tail ){Node *temp = Q[tail++];if( tail == QMAX ) tail = 0;Node *p = NULL;for( int i = 0;i < NUM;i++ ){if( temp->nxt[i] ){if( temp == root ) temp->nxt[i]->fail = root;else{p = temp->fail;while( p ){if( p->nxt[i] ){temp->nxt[i]->fail = p->nxt[i];break;}p = p->fail;}if( !p ) temp->nxt[i]->fail = root;}Q[head++] = temp->nxt[i];if( head == QMAX ) head = 0;}}}
}int query( Node *root )
{int result = 0, len = strlen( text );Node *p = root;for( int i = 0;i < len;i++ ){int nxtp = text[i] - ST;while( !p->nxt[nxtp] && p != root ) p = p->fail;p = p->nxt[nxtp];if( !p ) p = root;Node *temp = p;while( temp != root && temp->count != 0 ){result += temp->count;temp->count = 0;temp = temp->fail;}}return result;
}
void Release( Node *root )
{for( int i = 0;i < NUM;i++ ){if( root->nxt[i] ) Release( root->nxt[i] );}delete root;
}int t, n;
Node *root = NULL;
char str[51];int main()
{scanf( "%d",&t );while( t-- ){head = tail = 0;root = new Node();scanf( "%d",&n );while( n-- ){scanf( "%s",str );insert( str,root );}Build_AC( root );scanf( "%s",text );printf( "%d\n", query( root ) );//Release( root ); //销空间能免就省吧}
}
详细解决方案
HDU 2222 Keywords Search AC自动机 可做模板
热度:14 发布时间:2023-12-21 00:05:04.0
相关解决方案
- 关于-blank,parent,search,self,top的设置,该如何解决
- 再次提问:对路径“E:\小弟我的文档\Visual Studio 2005\WebSites\Search\crawled\segments”的访问被拒绝
- 一个关于google search api的有关问题,
- ASP.NET 怎么动态修改 Header 属性如添加 Meta 标签 keywords description
- 怎么在<meta name="keywords" content="$value[tagname]"/>加入关健词
- 怎的在*ascx页面里,设置它父页面的<meta name="keywords">
- asp 获取因特网址 title keywords description
- keywords、description在不同的浏览器中展示不一样
- 彻底剔除BabylonToolbar http://isearch.claro-search.com
- Head First Search Engine&1&Web Crawlers&bit地图
- jqGrid:6、 search
- Slash to Search―-更高速地搜索
- Web前端银行卡号分段输入成效(如:6225 7687 0971 2222)
- Web开发课程12-Hibernate Search
- Android怎么调用Google Web Search
- Google Local Search API ,本地map搜索
- javascript怎么查找指定"<a><b>1111</b><b>2222</b></a>"中的"<b>"与"</b>"的所有字符
- 哪位兄弟有Google Soap Search API的开发KEY,毕设急用,该如何处理
- hibernate.search 能的请进来
- hibernate search + 庖丁 不能生成索引文件!该如何处理
- Search for a range寻觅上下界-Leetcode
- eclipse导入项目时,付出提示 select a directory to search for existing Eclipse projects
- m2eclipse:Network connection problems encountered during search
- list = Employee.search("");这句什么意思啊。懂的人帮忙见见啊
- search bar和tableview结合的有关问题
- 如何才能实现iphone search bar style
- SQL Server FullText Search(MSSQLSERVER),该怎么处理
- 请教:sql server fulltext Search 是什么服务
- windbg symbol search path批改不了
- like ‘%” $search "%'‘%” 什么意思?该怎么处理