模板题,找来测代码。

注意有相同单词

//#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自动机的更多相关文章

  1. Substring Frequency (II) LightOJ - 1427 AC自动机

    https://vjudge.net/problem/LightOJ-1427 把所有模式串加入ac自动机,然后search的时候暴力,每个子串都暴力一下就好. 其实AC自动机就是,先建立好trie图 ...

  2. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  3. Codeforces 1015F Bracket Substring AC自动机 + dp

    Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...

  4. UVA11468 Substring --- AC自动机 + 概率DP

    UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...

  5. UVA-11468 Substring(AC自动机+DP)

    题目大意:给一些模板串,一些字符的出现概率.问不会出现模板串的概率是多少. 题目分析:是比较简单的概率DP+AC自动机.利用全概率公式递推即可. 代码如下: # include<iostream ...

  6. UVa 11468 (AC自动机 概率DP) Substring

    将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...

  7. Codeforces963C Frequency of String 【字符串】【AC自动机】

    题目大意: 给一个串s和很多模式串,对每个模式串求s的一个最短的子串使得这个子串中包含至少k个该模式串. 题目分析: 均摊分析,有sqrt(n)种长度不同的模式串,所以有关的串只有msqrt(n)种. ...

  8. 沉迷AC自动机无法自拔之:[UVA 11468] Substring

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这个鬼题目,上一波套路好了 先用题目给的模板串建\(AC\)自动机,把单词结尾标记为 \(val=1\),然后在建好的\(AC\)自动机上跑 \(dp\) ...

  9. UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...

随机推荐

  1. spring-security4.1.2的学习

    spring security教程 spring security是什么? Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了 ...

  2. 通过类名获取spring里的Bean

    import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactor ...

  3. C语言字符输出格式化

    转自http://blog.csdn.net/pbymw8iwm/article/details/8153226 符号属性 长度属性 基本型 所占 位数 取值范围 输入符举例 输出符举例 -- -- ...

  4. Rsa加解密Java、C#、php通用代码 密钥转换工具

    之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...

  5. EM算法(3):EM算法运用

    目录 EM算法(1):K-means 算法 EM算法(2):GMM训练算法 EM算法(3):EM算法运用 EM算法(4):EM算法证明 EM算法(3):EM算法运用 1. 内容 EM算法全称为 Exp ...

  6. Tomcat和JavaWeb笔记

    <iframe src="http://www.xmind.net/embed/XFXh" width="900px" height="540p ...

  7. SpringBoot-Learning

    SpringBoot-Learning 本项目内容为Spring Boot教程程序样例. 作者博客:http://blog.didispace.com Spring Boot系列博文:http://b ...

  8. python基础知识8——模块1——自定义模块和第三方开源模块

    模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...

  9. ipv6过审函数

    int _ResolveIPv4Address(const char* ipv4_str, char* buffer, int bufferSize) { struct addrinfo hints, ...

  10. XproerIM-v1.3更新-企业即时通迅

    版权所有 2009-2016 荆门泽优软件有限公司 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/apps/xproerim/index.a ...