模板题,找来测代码。

注意有相同单词

//#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. ue4标签测试与总结(UPROPERTY)

    学习UE4框架中的标签,本篇是总结成员变量标签UPROPERTY. 引擎版本:4.12.5 前期准备: 1.新建项目,C++空模板,新建C++类,继承AActor,名称MyActor. 使用TestA ...

  2. php工作笔记6-手机端适应缩放

    1.静态页面

  3. IDEA springMVC - hello world

    记录所学,防忘记... ide用IDEA,用maven管理依赖包 1.建立一个maven-webapp项目:File->New->Project 2.pom.xml <project ...

  4. 39、重新复习js之三

    1.盒子模型典型标签 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...

  5. background-position还可以这样用

    文章同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401626453&idx=1&sn=6af07 ...

  6. [转] --- Error: “A field or property with the name was not found on the selected data source” get only on server

    Error: “A field or property with the name was not found on the selected data source” get only on ser ...

  7. 通过属性 Cesium的FBO主要支持两种方式

    角色其实就是一类权限的分组,所以给用户分配角色其实也是在给用户分配权限.在oracle中有三个比较常用的角色.对于一般不是很严格的系统可以授予开发用户CONNECT.RESOURCE角色权限即可. 其 ...

  8. 如何捕获access violation异常

    文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...

  9. Python第一模块

    一.Python简介 二.Python种类 三.Python环境  windows: 1.需要配置环境变量 2.更新:卸载重装 linux:1.常用命令: 查看默认Python版本 Python -V ...

  10. 预测帖 苹果在2年之内会换Arm

    这几天看了iPad Pro和新的macbook, 我个人预测,苹果2年之内必换Arm处理器 首先会是笔记本. 而且会把软件移植也做的很好,不需要怎么改代码,只需要编译的时候加入相应的cpu就可以.至于 ...