题目链接:https://vjudge.net/problem/POJ-1625

Censored!
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10870   Accepted: 2979

Description

The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences.

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 first line of the input file contains three integer numbers: N -- the number of letters in Freish alphabet, M -- the length of all Freish sentences and P -- the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10).

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

Output the only integer number -- the number of different sentences freelanders can safely use.

Sample Input

2 3 1
ab
bb

Sample Output

5

Source

Northeastern Europe 2001, Northern Subregion

题意:

给出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 + 大数的更多相关文章

  1. [POJ1625]Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10824   Accepted: 2966 Descri ...

  2. Match:Censored!(AC自动机+DP+高精度)(POJ 1625)

     Censored! 题目大意:给定一些字符,将这些字符组成一个固定长度的字符串,但是字符串不能包含一些禁词,问你有多少种组合方式. 这是一道好题,既然出现了“一些”禁词,那么这题肯定和AC自动机有点 ...

  3. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

  4. POJ 1625 Censored!(AC自动机 + DP + 大数 + 拓展ASCII处理)题解

    题意:给出n个字符,p个病毒串,要你求出长度为m的不包含病毒串的主串的个数 思路:不给取模最恶劣情况$50^{50}$,所以用高精度板子.因为m比较小,可以直接用DP写. 因为给你的串的字符包含拓展A ...

  5. POJ1625 Censored!(AC自动机+DP)

    题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...

  6. 对AC自动机+DP题的一些汇总与一丝总结 (2)

    POJ 2778 DNA Sequence (1)题意 : 给出m个病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 关键字眼:不包含,个数,长度 DP[i][j] : 表示长 ...

  7. HDU2296 Ring(AC自动机+DP)

    题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...

  8. HDU2457 DNA repair(AC自动机+DP)

    题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...

  9. hdu 4117 GRE Words AC自动机DP

    题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...

随机推荐

  1. Tomcat 性能监控及调优

    1.性能监控 方式1: /usr/local/tomcat7/conf/tomcat-users.xml 添加如下: <role rolename="manager-gui" ...

  2. m3u8文件信息总结

    近期在做视频下载.本地播放功能的时候.发现的问题,先笔记记录一下 开发思路 (1) 在线解析m3u8文件内容,把里面的ts相应连接的资源下载本地的Document文件下. (2) 把下载下来的资源使 ...

  3. EXCel鼠标右键不能用解决办法

    EXCel鼠标右键不能用解决办法 倒腾vba首要是保证安全,各路大神的代码非常神奇,莫名的就让你的excel嘎嘣了,如出现右键无法使用(确定不是您的鼠标问题),那么以下代码可完全修复设置.操作步骤:打 ...

  4. redmine 自己定义字段mysql表结构

    redmine能够创建自己定义字段,我经经常使用它来满足不同的管理需求.如今来解读一下.看看这些自己定义字段是怎样存在mysql表中的. 表issues 用来存放issue的标准字段. mysql&g ...

  5. js判断参数类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  6. js new一个函数和直接调用函数的差别

    用new和调用一个函数的差别:假设函数返回值是一个值类型(Number.String.Boolen)时,new函数将会返回这个函数的实例对象.而假设这个函数的返回值是一个引用类型(Object.Arr ...

  7. iOS 逆向 - Class-dump 安装和使用方法

    1.下载安装包 http://stevenygard.com/projects/class-dump/,这里我下载的是 class-dump-3.5.dmp.然后把下载下来的 dmg 打开,复制文件里 ...

  8. MySQL mysqlbinlog

    MySQL binlog日志记录了MySQL数据库从启用日志以来所有对当前数据库的变更.binlog日志属于二进制文件,我们可以从binlog提取出来生成可读的文本或者SQL语句来重建当前数据库以及根 ...

  9. about gnu bash shell

    1 定义字符串不需要引号 var=NONE echo $var ==>NONE 2 支持基本的整数计算 a=1 b=2 echo $((a+b)) ==>3 必须用$(()),双括号的形式 ...

  10. Greedy Function Approximation:A Gradient Boosting Machine

    https://statweb.stanford.edu/~jhf/ftp/trebst.pdf page10 90% to 95% of the observations were often de ...