hdu 2896 ac自动机(last指针)
来源:互联网 发布:notepad mac 破解 编辑:程序博客网 时间:2024/06/15 07:47
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <vector>using namespace std;#define mxn 100020struct trie {int ch[mxn][128], fail[mxn], lst[mxn], cnt[mxn];int sz;int creat() {memset( ch[sz], -1, sizeof( ch[sz] ) );cnt[sz] = 0;return sz++;}void init() {memset( ch[0], -1, sizeof( ch[0] ) );cnt[0] = 0;sz = 1;}void insert( char *s, int val ) {int t = 0;for( int i = 0; s[i]; ++i ) {int c = s[i];if( ch[t][c] == -1 )ch[t][c] = creat();t = ch[t][c];}cnt[t] = val;}void build() {queue<int> q;fail[0] = 0;for( int i = 0; i < 128; ++i ) {if( ch[0][i] == -1 )ch[0][i] = 0;else {fail[ch[0][i]] = 0;q.push( ch[0][i] );}}while( !q.empty() ) {int t = q.front(); q.pop();for( int i = 0; i < 128; ++i ) {if( ch[t][i] == -1 )ch[t][i] = ch[fail[t]][i];else {int nxt = ch[t][i];fail[nxt] = ch[fail[t]][i];lst[nxt] = cnt[fail[nxt]] ? fail[nxt]: lst[fail[nxt]];q.push( nxt );}}}}bool query( char *s, int num ) {vector<int> g;int t = 0;for( int i = 0; s[i]; ++i ) {int c = s[i];t = ch[t][c];int tmp = t;while( tmp ) {if( cnt[tmp] ) {g.push_back( cnt[tmp] );}tmp = lst[tmp];}}if( g.size() == 0 )return 0;printf( "web %d:", num );sort( g.begin(), g.end() );g.erase( unique( g.begin(), g.end() ), g.end() );for( int i = 0; i < g.size(); ++i )printf( " %d", g[i] );puts( "" );return 1;}void debug() { for( int i = 0;i < sz; i++ ) { printf( "id = %3d,fail = %3d,cnt = %3d,chi = [", i, fail[i], cnt[i] ); for( int j = 0; j < 26; j++ ) printf( "%2d",ch[i][j] ); printf( "]\n" ); } }}ac;char s[100020];int main() {int n;while( scanf( "%d", &n ) != EOF ) {ac.init();getchar();for( int i = 1; i <= n; ++i ) {gets( s );ac.insert( s, i );}ac.build();scanf( "%d", &n );int ans = 0;getchar();for( int i = 1; i <= n; ++i ) {gets( s );if( ac.query( s, i ) )ans++;}printf( "total: %d\n", ans );}return 0;}
0 0
- hdu 2896 ac自动机(last指针)
- hdu 2896 AC自动机(指针+数组)
- HDU 2896 AC自动机
- HDU 2896 AC自动机
- hdu 2896 ac自动机
- HDU 2896 AC自动机
- HDU 2896 AC自动机
- HDU 2896 AC自动机
- hdu 2896 AC自动机
- hdu 2896 ac自动机
- hdu 2896(ac自动机)
- HDU 2896 AC自动机
- AC 自动机 HDU 2896
- HDU 2896 (AC自动机)
- hdu 2896AC自动机
- HDU 2896 AC自动机
- hdu 2896 ac自动机
- HDU-2896-AC自动机
- HDU 1069 Monkey and Banana
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta
- oracle常见问题
- 解决NLPIR-ICTCLAS2014分词系统词库过大,造成读取缓慢问题
- uinty脚本执行顺序
- hdu 2896 ac自动机(last指针)
- 浅谈设计模式
- IE(IE6/IE7/IE8)支持HTML5标签
- ScrollView中嵌套ListView时显示不全的简便解决方案
- 华为so-胜负猜想 动态规划复习
- 时序图用法入门
- linux修改时间
- 实时数据仓库
- WIN32 GUI 滚动条 简单实例