链接   http://acm.fzu.edu.cn/problem.php?pid=2128

解题方法  首先考虑暴力,,就是拿每一个字符串在匹配串里面找到所有位置,然后从头到尾不断更新最长的合理位置pos 同时记录出 最长的长度; 想到这样会超时,所以 AC 自动机一下,找到任意一个后缀最长能匹配的位置 方法是: 在构建自动机的 fail 指针的时候,找到任意位置的任意字符最长公共前缀的时候看那个位置是否有单词存在;

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; struct date{
date *next[26],*fail;
int len; bool flag;
}tree[100005],*que[100005],*root;
int N,total,tail,hed; char str[1000006],cha[102];
date *creat( )
{
for( int i = 0; i < 26; i++ )
tree[total].next[i] = NULL;
tree[total].fail = NULL;
tree[total].flag = false;
tree[total].len = 1000000;
return &tree[total++];
}
void inint( )
{
total = hed = tail = 0;
root = creat();
root->fail = root;
}
void insert( char *word,int len )
{
date *temp = root;
while( *word )
{
int num = *word - 'a';
if( temp->next[num] == NULL )
temp->next[num] = creat();
temp = temp->next[num];
word++;
}
temp->len = len-1;
temp->flag = true;
}
void build( )
{
que[tail++] = root;
while( tail > hed )
{
date *temp = que[hed++];
for( int i = 0; i < 26; i++ )
if( temp->next[i] != NULL ){
if( temp == root )temp->next[i]->fail = root;
else temp->next[i]->fail = temp->fail->next[i];
if( temp->fail->next[i]->flag )
temp->next[i]->len = min( temp->next[i]->len,temp->fail->next[i]->len );
que[tail++] = temp->next[i];
}
else {
if( temp == root )temp->next[i] = root;
else temp->next[i] = temp->fail->next[i];
}
}
}
short int res[1000006];
inline int max( int a,int b){return a>b?a:b;}
inline int min( int a,int b){return a>b?b:a;}
void search( char *word )
{
int t = 0; date *temp = root; int len = strlen(word);
while( *word )
{
int num = *word - 'a';
temp = temp->next[num];
res[t] = temp->len;
word++; t++;
}
int pre = 0; int Max = 0;
for( int i = 0; i < len; i++ ){
pre = min( pre + 1,res[i] );
Max = max( Max,pre );
}
printf("%d\n",Max);
}
int main( )
{
while( scanf("%s",&str) != EOF )
{
scanf("%d",&N); inint( );
for( int i = 1; i <= N; i++ ){
scanf("%s",&cha); insert(cha,strlen(cha));
}
build( ); search( str );
}
return 0;
}

  

fzu 2128 AC自动机的更多相关文章

  1. fzu 2246(ac 自动机)

    fzu 2246(ac 自动机) 题意: 某一天YellowStar学习了AC自动机,可以解决多模式匹配问题.YellowStart当然不会满足于此,它想进行更深入的研究. YellowStart有一 ...

  2. HDU 3341 Lost's revenge AC自动机+dp

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  3. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  4. AC自动机-算法详解

    What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...

  5. python爬虫学习(11) —— 也写个AC自动机

    0. 写在前面 本文记录了一个AC自动机的诞生! 之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的.. C# 逆袭--自制日刷千题的AC自动机攻克HDU OJ HDU 自 ...

  6. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  7. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  8. BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]

    1212: [HNOI2004]L语言 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1367  Solved: 598[Submit][Status ...

  9. [AC自动机]【学习笔记】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...

随机推荐

  1. ExtJs之Element.select函数

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  2. Android图片缩放方法

    安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...

  3. BZOJ 1046: [HAOI2007]上升序列 LIS -dp

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3438  Solved: 1171[Submit][Stat ...

  4. dom对象详解--document对象(一)

     document对象 Document对象代表整个html文档,可用来访问页面中的所有元素,是最复杂的一个dom对象,可以说是学习好dom编程的关键所在. Document对象是window对象的一 ...

  5. 项目中遇到的 linq datatable select

    1如何使用DataTable.Select选出来的Rows生成新的DataTable?DataTable dt = 数据源;DataTable dtt = new DataTable();dtt=dt ...

  6. lintcode: 生成括号

    生成括号 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果. 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()())&q ...

  7. win7 安装Redis

    1.下载Redis的压缩包 https://github.com/dmajkic/redis/downloads 我下载的是redis-2.4.5-win32-win64.zip 下载完后将其解压放在 ...

  8. ArcGIS Runtime SDK for Android 10.2.5新开发平台安装配置指南

    ArcGIS Runtime SDK for Android 10.2.5版本在年前发布,其中一个重大的变化是:新版本使用了新的开发环境,在10.2.5版本中Esri使用了官方提供的新的Android ...

  9. CentOS增加硬盘

    1.查看新硬盘     #fdisk –l      新添加的硬盘的编号为/dev/sdb 2.硬盘分区     1)进入fdisk模式     #/sbin/fdisk /dev/sdb     2 ...

  10. Partitioner

    partitioner 是map中的数据映射到不同的reduce时的根据.一般情况下,partitioner会根据数据的key来把数据平均分配给不同的reduce,同时保证相同的key分发到同一个re ...