题意:

  求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. 微信支付 - V3退款

        退款问题: 1.证书加载不进去,出现"内部错误" 解决:在iis中找到对应的应用连接池,右键高级设置,找到"加载用户配置文件"改为true.   2.需 ...

  2. 苹果开发者账号申请时报错提示错误:Legal Entity Name

    he information you entered did not match your profile in the D&B database. Before submitting you ...

  3. Web框架之Tornado

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

  4. 解决MYSQL错误:ERROR 1040 (08004): Too many connections

    方法一: show processlist; show variables like 'max_connections'; show global status like 'max_used_conn ...

  5. 命令行登陆Oracle(包括远程登陆)

    本方法适用于在cmd命令行窗口以及pl/sql登陆Oracle下登录本机或者远程Oracle. 1.首先保证在当前主机上设置了ORACLE_HOME环境变量:     例如:ORACLE_HOME=D ...

  6. apache lucene solr 官网历史版本下载地址

    官网上一般只提供最新版本的下载,下面两个链接为所有历史版本的下载地址: lucene地址:archive.apache.org/dist/lucene/java/ solr地址:archive.apa ...

  7. android 入门-android Studio 快捷输入

    1.输入 log的时候按一下Tab.就会打出 private static final String TAG="Settings"; 2. shift +alt+x 运行 shif ...

  8. [Tools] 使用XP远程登录Win8系统

    [背景] 完成最基本的设置后,发现xp依然不能远程访问win8桌面,搜索后发现需要进一步设置   [开工] 按照参考资料进行设置,下面的参考资料已经写的很详细了,只是参考资料2中的文件名: redss ...

  9. 汇编学习(一)——win7 64位调出debug

    一.安装方法: 1.下载一个dosbox和win7 32位debug.exe,安装dosbox,打开页面 2. 将debug.exe放入磁盘根目录,这里以D盘为例.在dosbox中输入mount c ...

  10. Memcache升级版:CouchBase的安装配置与使用说明

    Memcache基本上已经是开发的标配了,但是对于Memcache集群,很多线上部署仍然是很单薄的. 几个存在的问题:不健壮.数据不安全.配置变更可能导致存取异常.后备数据的一致性 鉴于存在以上问题, ...