题意:

  求n个模板串在匹配串中出现了几个.

SOL:

  反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同...

  貌似自己的理解和他们画的图还是有些出入......不虚慢慢看...

  然后就是特殊一点的一个last数组,可以比较迅速地找到包含的子串.

  这个题目会出现相同的模板...没看懂老人家开map的意图,第一遍用vector打,然后这种统计数量不是直接开个num记录数量就好了吗...

  然而为毛我的num比开vector还慢呢...

  

/*==========================================================================
# Last modified: 2016-03-02 19:00
# Filename: a.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 1000200
#define maxm 100000
#define maxc 30
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
char temp[maxn],s[60];
int ch[500005][maxc],f[500005],last[500005];
vector<int> val[500005];
int sz,ans;
void reset(){
sz=1; ans=0;
memset(ch,0,sizeof(ch)); memset(last,0,sizeof(last));
memset(val,0,sizeof(val)); memset(f,0,sizeof(f));
}
int idx(char c){return c-'a';}
void insert(char *s,int v){
int u=0,len=strlen(s);
FORP(i,0,len-1){
int c=idx(s[i]);
if (!ch[u][c]) {
while(val[sz].size()) val[sz].pop_back();
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u].push_back(v);
}
void add(int j){
while(val[j].size()){
ans+=val[j].size();
while (val[j].size()) val[j].pop_back();
j=last[j];
}
}
void find(char *T){
int n=strlen(T);
int j=0;
FORP(i,0,n-1){
int c=idx(T[i]);
j=ch[j][c];
if (val[j].size()) add(j);
else if(last[j]) add(last[j]);
}
}
void getfail(){
queue<int>q;
f[0]=0;
FORP(c,0,maxc-1){
int u=ch[0][c];
if (u) {
f[u]=0; last[u]=0;
q.push(u);
}
}
while (!q.empty()){
int r=q.front(); q.pop();
FORP(c,0,maxc-1){
int u=ch[r][c];
if (!u) {ch[r][c]=ch[f[r]][c]; continue;}
q.push(u);
int v=f[r];
while (v && !ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]].size()?f[u]:last[f[u]];
}
}
}
int main(){
int cas; read(cas);
while (cas--){
reset();
int n; read(n);
FORP(i,1,n){
scanf("%s",s);
insert(s,i);
}
scanf("%s",temp);
getfail();
find(temp);
printf("%d\n",ans);
}
}

HDU 2222 & ac自动机模板的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  3. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  4. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  5. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  6. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  7. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  8. hdu 2222(AC自动机模版题)

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

  9. hdu 2222 ac自动机更新模板 for onSite contest

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...

随机推荐

  1. MyEclipse生成WAR包并在Tomcat下部署发布(转发)

    从来没有想过web项目还能打包的,但是有要求,就不得不去实现,在网上找了一下,发现挺简单的. 首先是使用MyEclipse将web项目打包,如下图所示. 右键选中项目,选择export. 然后选择J2 ...

  2. 1 Ionic和Hybird应用介绍

    1.Ionic是什么,它和Angular.Cordova有什么关系? Ionic通过整合各种技术和功能使构建Hybird应用更加快速.容易和美观.Ionic生态系统基于Angular和Cordova, ...

  3. 关于v$datafile中system表空间的status值始终为system

    http://docs.oracle.com/cd/B19306_01/server.102/b14237/dynviews_1076.htm#REFRN30050 http://blog.itpub ...

  4. 最稳定 性能最好 的 Linux 版本?

    Ubuntu太他妈不稳定了,简直是一坨屎 CentOS.Ubuntu.Debian三个linux比较异同http://blog.csdn.net/educast/article/details/383 ...

  5. [LeetCode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. 不定义JQuery插件,不要说会JQuery 分类: JavaScript 2014-11-24 14:18 155人阅读 评论(0) 收藏

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...

  7. 使用SOUI开发的界面集锦

    仿QQ管家界面

  8. 在windows系统的文件右键菜单中增加“命令提示符”

    本实用小工具能够在windows系统的文件右键菜单中增加“命令提示符”,方便快速进入制定文件的命令提示窗口,避免逐层输入或复制文件夹路径,极其实用. 工具下载地址如下:360云盘(访问密码:5b71) ...

  9. javase基础笔记1——简介和发展

    软件分为 系统软件 windows linux类 (unix)mac(麦金塔).数据库管理系统 unix linux 开源os(open source) 免费 开放 free os operation ...

  10. ado.net增删改查练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...