light oj 1427(ac自动机)
#include <bits/stdc++.h> using namespace std; const int N = *;
const int M = ; map<string,int>Map;
struct Trie
{
int next[N][M],fail[N],end[N];
int root,L;
int newnode()
{
for(int i = ; i < ; i++)
next[L][i] = -;
end[L++] = -;
return L - ;
}
void init()
{
L = ;
root = newnode();
}
void insert(string s,int id)
{
int len = s.size();
int now = root;
for(int i = ; i < len; i++)
{
if(next[now][s[i]-'a'] == -)
next[now][s[i] - 'a'] = newnode();
now = next[now][s[i] - 'a'];
}
end[now] = id;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < ; i++)
{
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = ; i < ; i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int num[];
void query(char buf[],int n,int mm[])
{
for(int i = ; i <= n; i++)
num[i] = ;
int len = (int)strlen(buf);
int now = root;
for(int i = ; i < len; i++)
{
now = next[now][buf[i]-'a'];
int temp = now;
while( temp != root )
{
if( end[temp] != -)
num[end[temp]]++;
temp = fail[temp];
}
}
for(int i = ; i <= n; i++)
printf("%d\n",num[mm[i]]);
}
}; char buf[];
Trie ac; void solve()
{
ac.init();
int n, l = ;
scanf("%d",&n);
scanf("%s",buf);
Map.clear();
int twice[];
memset(twice,,sizeof(twice));
for(int i = ; i <= n; i++)
{
string ss;
cin>>ss;
int c = Map[ss];
if(c == )
{
Map[ss] = ++l;
ac.insert(ss,l);
}
twice[i] = Map[ss];
//int tmp = Map[ss]; }
ac.build(); ac.query(buf,n,twice);
} int main(void)
{
int t,cnt = ;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",++cnt);
solve();
} return ;
}
light oj 1427(ac自动机)的更多相关文章
- Substring Frequency (II) LightOJ - 1427 AC自动机
https://vjudge.net/problem/LightOJ-1427 把所有模式串加入ac自动机,然后search的时候暴力,每个子串都暴力一下就好. 其实AC自动机就是,先建立好trie图 ...
- [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ
前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...
- python爬虫学习(11) —— 也写个AC自动机
0. 写在前面 本文记录了一个AC自动机的诞生! 之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的.. C# 逆袭--自制日刷千题的AC自动机攻克HDU OJ HDU 自 ...
- AC自动机基础知识讲解
AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...
- Aho_Corasick自动机(AC自动机)
首先,AC自动机不是Accept自动机,别以为把这段代码复制到OJ上就全都自动AC了…… 其实这玩意是Aho-Corasick 造出来的,所以你懂的. 那么这玩意能干嘛咧? •字符串的匹配问题 •多串 ...
- 字符串处理-AC自动机
估计在OJ上刷过题的都会对AC自动机这个名词很感兴趣,同样,记得去年ACM暑期集训的时候,在最后讲到字符串部分,听说了这个算法的名字之后就对于它心向往之,AC正好是Accept的简称,字面意义上的理解 ...
- 【模版】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...
- AC自动机模板3【洛谷3796】
AC自动机的第三个模板 其实,个人觉得,目前我写的这三个不同的模板完全是可以合并在一起求解的. 只是,在这两个无关联的OJ上,同一个AC自动机都可以完成的问题被拆成了三道题而已. 因此,代码只需要略加 ...
- AC自动机模板1(【洛谷3808】)
题面 题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串 ...
随机推荐
- 廖雪峰Java14Java操作XML和JSON-1XML-3SAX
SAX:Simple API for XML 基于事件的API import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPars ...
- thinkphp 视图定义
视图定义 视图通常是指数据库的视图,视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的 ...
- 字符串+dp——cf1163D好题
很好的题(又复习了一波kmp) /* dp[i,j,k]:到s1的第i位,匹配s2到j,s3到k的最优解 */ #include<bits/stdc++.h> using namespac ...
- php日期
PHP Date() 函数 PHP date() 函数用于格式化时间/日期. 该函数可把时间戳格式化为可读性更好的日期和时间. 时间戳是一个字符序列,表示一定的事件发生的日期/时间. 语法 date( ...
- CF #575 Div3
// 比赛链接:https://codeforces.com/contest/1196 // CF 2019.7.24 // 本想Div3手速场上分,结果卡在C题,掉了不少分. // 自闭了这么久,今 ...
- 《DSP using MATLAB》Problem 7.38
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- 从一个prismWpfMVVM的例子中学到的
整个程序如下,从博客园一个作者看到的例子,但是对这个例子做了点修改.我觉得这个更符合MVVM模式.这个用到了prism框架,在项目中要引用Microsoft.Practices.Prism.dll 按 ...
- boost 字符串大小写转换
示例代码如下: #include <boost/algorithm/algorithm.hpp> #include <iostream> using namespace std ...
- 2、设备树的规范(dts和dtb格式)
第01节_DTS格式(1) 语法:Devicetree node格式:[label:] node-name[@unit-address] { [properties definitions] ...
- 常见的5个runtime exception
NullPointException(空指针异常),ArrIndexOutOfBoundsException(数组越界异常),ClassCastException(类型转换异常),ClassNotFo ...