lightoj 1427 - Substring Frequency (II) AC自动机
模板题,找来测代码。
注意有相同单词
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a, const T& b, const T& c)
{
return min(min(a, b), c);
}
template<class T> T max(const T& a, const T& b, const T& c)
{
return max(max(a, b), c);
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
int ch;
while((ch = getchar()) != EOF)
{
if(ch!=' ' && ch!='\n') return ch;
}
return EOF;
} const int max_len = ;
const int max_node = max_len * max_len;
const int sigma_size = ; struct ac_automation
{
int sz;
int ch[max_node][sigma_size];
int fail[max_node];
vector<int> val[max_node];
int last[max_node]; void init()
{
memset(ch[], , sizeof(ch[]));
val[].clear();
sz = ;
}
int id(char c)
{
return c - 'a';
}
void insert(const char *s, int v)
{
int u = ;
for(int i = ; s[i] != '\0'; i++)
{
int v = id(s[i]);
if(!ch[u][v])
{
memset(ch[sz], , sizeof(ch[sz]));
val[sz].clear();
ch[u][v] = sz++;
}
u = ch[u][v];
}
val[u].push_back(v);
} void construct()
{
queue<int> q;
fail[] = ;
for(int c = ; c < sigma_size; c++)
{
int u = ch[][c];
if(u) {q.push(u); fail[u] = ; last[u] = ; }
} while(!q.empty())
{
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++)
{
int u = ch[r][c];
if(u)
{
q.push(u);
fail[u] = ch[fail[r]][c];
last[u] = (!val[fail[u]].empty()) ? fail[u] : last[fail[u]];
} else ch[r][c] = ch[fail[r]][c];
}
}
} void count(int x[], int u)
{
if(val[u].size() > )
{
for(int i = ; i < val[u].size(); i++)
x[val[u][i]]++;
count(x, last[u]);
}
}
void find(const char *T, int x[])
{
int u = ;
for(int i = ; T[i] != '\0'; i++)
{
int v = id(T[i]);
u = ch[u][v];
if(!val[u].empty()) count(x, u);
else if(last[u]) count(x, last[u]);
}
}
}; ac_automation solver;
const int maxn = ;
char T[maxn];
int ans[maxn]; int main()
{
debug();
int t;
scanf("%d", &t);
for(int ca = ; ca <= t; ca++)
{
solver.init();
int n;
scanf("%d%s", &n, T);
for(int i = ; i <= n; i++)
{
char word[max_len];
scanf("%s", word);
solver.insert(word, i);
}
solver.construct(); memset(ans, , sizeof(ans));
solver.find(T, ans); printf("Case %d:\n", ca);
for(int i = ; i <= n; i++)
{
printf("%d\n", ans[i]);
}
}
return ;
}
lightoj 1427 - Substring Frequency (II) AC自动机的更多相关文章
- Substring Frequency (II) LightOJ - 1427 AC自动机
https://vjudge.net/problem/LightOJ-1427 把所有模式串加入ac自动机,然后search的时候暴力,每个子串都暴力一下就好. 其实AC自动机就是,先建立好trie图 ...
- Substring UVA - 11468 AC自动机+概率DP
题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- UVA11468 Substring --- AC自动机 + 概率DP
UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...
- UVA-11468 Substring(AC自动机+DP)
题目大意:给一些模板串,一些字符的出现概率.问不会出现模板串的概率是多少. 题目分析:是比较简单的概率DP+AC自动机.利用全概率公式递推即可. 代码如下: # include<iostream ...
- UVa 11468 (AC自动机 概率DP) Substring
将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...
- Codeforces963C Frequency of String 【字符串】【AC自动机】
题目大意: 给一个串s和很多模式串,对每个模式串求s的一个最短的子串使得这个子串中包含至少k个该模式串. 题目分析: 均摊分析,有sqrt(n)种长度不同的模式串,所以有关的串只有msqrt(n)种. ...
- 沉迷AC自动机无法自拔之:[UVA 11468] Substring
图片加载可能有点慢,请跳过题面先看题解,谢谢 这个鬼题目,上一波套路好了 先用题目给的模板串建\(AC\)自动机,把单词结尾标记为 \(val=1\),然后在建好的\(AC\)自动机上跑 \(dp\) ...
- UVa 11468 Substring (AC自动机+概率DP)
题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...
随机推荐
- 《循序渐进》之简单的DHCP实验
目的:初学,配置简单的DHCP试验. 试验步骤: 1:拓扑图 2:配置: Router>en Router#config t Enter configuration commands, one ...
- .NET 工具类ObjectDumper 打印对象
// Comes from the LINQ samples provided by Microsoft //Copyright (C) Microsoft Corporation. All righ ...
- php工作笔记7-概率算法
a/m b/m c/m d/m 10% 40% 20% a+b+c+d+... < = m array k = {a,b,c...} randt = rand(1 ...
- Datasnap Image
delphi用,不能与java.c#互相识别. procedure TServerMethods.UpdateDoc(ItemID : integer; doc : TStream); delphi用 ...
- boost.numpy编译报错:undefined reference to `PyInt_FromLong' libboost_numpy.so: undefined reference to `PyCObject_AsVoidPtr'
[ 31%] Built target boost_numpy[ 36%] Building CXX object libs/numpy/example/CMakeFiles/dtype.dir/dt ...
- WEB框架
WEB框架本质 一.WEB请求流程 所有的web应用,都 ...
- win10无法使用内置管理员账户打开应用怎么办
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System \UIPI 右边有个默认的项.将它的值改成1
- bootstrap--小李子demo
最近忙啊...看到各位冬鞋都在认真写博客,认真敲代码,认真工作,总觉得自己时间太少,总觉得时间不够,老了...... 进正题: 上次不知从哪里(忘了)下载了bootstrap的一些使用小demo,以后 ...
- Python+Google Geocoding
本文主要介绍使用Python调用Google Geocoding API进行地址到地理坐标的转换. Google Geocoding参考https://developers.google.com/ma ...
- 深入理解js——作用域和上下文环境
如图除全局作用域外,每个函数都会创建自己的作用域.作用域在函数定义时就确定了,而不是在函数调用时确定. 下面按照程序执行的步骤加上上下文环境. 第一步:程序加载时已经确定全局上下文环境,并随着程序的执 ...