POJ1625 Censored! —— AC自动机 + DP + 大数
题目链接:https://vjudge.net/problem/POJ-1625
| Time Limit: 5000MS | Memory Limit: 10000K | |
| Total Submissions: 10870 | Accepted: 2979 | 
Description
But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1], S[k+1] = W[2], ...,S[k+len(W)-1] = W[len(W)], where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years.
Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it.
Input
The second line contains exactly N different characters -- the letters of the Freish alphabet (all with ASCII code greater than 32).
The following P lines contain forbidden words, each not longer than min(M, 10) characters, all containing only letters of Freish alphabet.
Output
Sample Input
2 3 1
ab
bb
Sample Output
5
Source
题意:
给出p个单词,求长度为m且不含有已给单词的字符串有多少个?其中字母表有n个字母。
题解:
与POJ2278 DNA Sequence无异,只不过此题没有取模,而答案可能很大,所以用到高精度。而且字符串长度较小,可以直接用动态规划进行求解,而不需要用到矩阵进行加速。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = *+; struct BigInt
{
const static int mod = ;
const static int DLEN = ;
int a[],len;
BigInt()
{
memset(a,,sizeof(a));
len = ;
}
BigInt(int v)
{
memset(a,,sizeof(a));
len = ;
do
{
a[len++] = v%mod;
v /= mod;
}while(v);
}
BigInt operator +(const BigInt &b)const
{
BigInt res;
res.len = max(len,b.len);
for(int i = ;i <= res.len;i++)
res.a[i] = ;
for(int i = ;i < res.len;i++)
{
res.a[i] += ((i < len)?a[i]:)+((i < b.len)?b.a[i]:);
res.a[i+] += res.a[i]/mod;
res.a[i] %= mod;
}
if(res.a[res.len] > )res.len++;
return res;
}
void output()
{
printf("%d",a[len-]);
for(int i = len-;i >= ;i--)
printf("%04d",a[i]);
printf("\n");
}
}; BigInt dp[][MAXN];
int M[];
struct Trie
{
int sz, base;
int next[MAXN][], fail[MAXN], end[MAXN];
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = false;
return L-;
} void init(int _sz, int _base)
{
sz = _sz;
base = _base;
L = ;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][M[buf[i]]] == -) next[now][M[buf[i]]] = newnode();
now = next[now][M[buf[i]]];
}
end[now] |= true;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; 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();
end[now] |= end[fail[now]];
for(int i = ; i<sz; 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]);
}
}
} void query(int len)
{
for(int i = ; i<=; i++)
for(int j = ; j<L; j++)
dp[i][j] = ; int cur = ;
dp[cur][root] = ;
for(int i = ; i<len; i++)
{
for(int j = ; j<L; j++)
dp[cur^][j] = ;
for(int j = ; j<L; j++)
{
if(end[j]) continue;
for(int k = ; k<sz; k++)
{
if(end[next[j][k]]) continue;
dp[cur^][next[j][k]] = dp[cur^][next[j][k]] + dp[cur][j];
}
}
cur ^= ;
} BigInt ret = ;
for(int i = ; i<L; i++)
ret = ret + dp[cur][i];
ret.output();
}
}; Trie ac;
char buf[MAXN];
int main()
{
int n, m, p;
while(scanf("%d%d%d", &n,&m,&p)!=EOF)
{
ac.init(n, );
scanf("%s", buf);
for(int i = ; buf[i]; i++)
M[buf[i]] = i;
for(int i = ; i<=p; i++)
{
scanf("%s", buf);
ac.insert(buf);
}
ac.build();
ac.query(m);
}
}
POJ1625 Censored! —— AC自动机 + DP + 大数的更多相关文章
- [POJ1625]Censored!(AC自动机+DP+高精度)
		Censored! Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10824 Accepted: 2966 Descri ... 
- Match:Censored!(AC自动机+DP+高精度)(POJ 1625)
		Censored! 题目大意:给定一些字符,将这些字符组成一个固定长度的字符串,但是字符串不能包含一些禁词,问你有多少种组合方式. 这是一道好题,既然出现了“一些”禁词,那么这题肯定和AC自动机有点 ... 
- POJ 1625 Censored!(AC自动机+DP+高精度)
		Censored! Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6956 Accepted: 1887 Descrip ... 
- POJ 1625 Censored!(AC自动机 + DP + 大数 + 拓展ASCII处理)题解
		题意:给出n个字符,p个病毒串,要你求出长度为m的不包含病毒串的主串的个数 思路:不给取模最恶劣情况$50^{50}$,所以用高精度板子.因为m比较小,可以直接用DP写. 因为给你的串的字符包含拓展A ... 
- POJ1625 Censored!(AC自动机+DP)
		题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ... 
- 对AC自动机+DP题的一些汇总与一丝总结 (2)
		POJ 2778 DNA Sequence (1)题意 : 给出m个病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 关键字眼:不包含,个数,长度 DP[i][j] : 表示长 ... 
- HDU2296 Ring(AC自动机+DP)
		题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ... 
- HDU2457 DNA repair(AC自动机+DP)
		题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ... 
- hdu 4117 GRE Words AC自动机DP
		题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ... 
随机推荐
- API测试工具postman使用总结
			一.Postman介绍: Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件,主要用于模拟网络请求包,快速创建请求,回放.管理请求,验 ... 
- Java数据结构-线性表之静态链表
			静态链表的定义: 节点由一个一维数组和一个指针域组成,数组用来存放数据元素,而指针域里面的指针(又称游标)用来指向下一个节点的数组下标. 这种链表称之为静态链表. 链表中的数组第一个和最后一个位置须要 ... 
- Vue 中computed 与 methods 区别
			1.示例 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF- ... 
- unity 切圆角矩形  --shader编程
			先上个效果图 制作思路 如上图我们要渲染的就是上图带颜色的部分 步骤: 先获取黄色和蓝绿部分 例如以下图 算法 |U|<(0.5-r)或|V|<(0.5-r) 注意的是模型贴图最大值是1. ... 
- SQL Server -使用表触发器记录表插入,更新,删除行数
			1.如何使用sql获取当前session用户名和机器名 Select CURRENT_USER,Host_name() 2.如何在表触发器中获取当前表名称 SELECT OBJECT_SCHEMA_N ... 
- TextView上的文字逐渐变淡直到消失
			给TextView加个动画效果,完了在个动画加个监听,里面有个动画执行完调用的方法在方法里面把TextView设置为gone,我觉得你直接加这个动画效果之后他就会不显示了,其实他还在那占有位置呢.想不 ... 
- 1verilog 位拼接
			位拼接还可以用重复法来简化表达式.见下例: {4{w}} //这等同于{w,w,w,w} 位拼接还可以用嵌套的方式来表达.见下例: {b,{3{a,b}}} //这等同 ... 
- Android 属性动画框架 ObjectAnimator、ValueAnimator ,这一篇就够了
			前言 我们都知道 Android 自带了 Roate Scale Translate Alpha 多种框架动画,我们可以通过她们实现丰富的动画效果,但是这些宽家动画却有一个致命的弱点,它们只是改变了 ... 
- jdbcTemplaate   queryForObject的两个易混淆的方法
			JdbcTemplate中有两个可能会混淆的queryForObject方法: 1. Object queryForObject(String sql, Object[] args, Class ... 
- PHP和mysql的长连接
			关于 PHP MySQL 长连接.连接池的一些探索 PHP连接MySQL的方式,用的多的是mysql扩展.mysqli扩展.pdo_mysql扩展,是官方提供的.php的运行机制是页面执行完会释放所有 ... 
