您的位置:首页 > 其它

POJ 1625 Censored!

2015-11-05 14:58 369 查看

Censored!

Time Limit: 5000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1625
64-bit integer IO format: %lld Java class name: Main

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

解题:Trie图 + 动态规划

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct BigInt {
const static int mod=10000;
const static int dlen=4;
int a[110],len;

BigInt() {
memset(a,0,sizeof(a));
len=1;
}

BigInt(int v) {
memset(a,0,sizeof(a));
len=0;
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=0; i<=res.len; i++) {
res.a[i]=0;
}
for(int i=0; i<res.len; i++) {
res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
res.a[i+1]+=res.a[i]/mod;
res.a[i]%=mod;
}
if(res.a[res.len]>0) res.len++;
return res;
}

void output() {
printf("%d",a[len-1]);
for(int i=len-2; i>=0; i--)
printf("%04d",a[i]);
printf("\n");
}
} dp[110][110];
const int maxn = 256;
struct Trie{
int ch[maxn][maxn],fail[maxn],cnt[maxn],tot,n;
int mp[256];
int newnode(){
memset(ch[tot],0,sizeof ch[tot]);
fail[tot] = cnt[tot] = 0;
return tot++;
}
void init(char *str,int n){
tot = 0;
for(int i = 0; i < n ; ++i) mp[str[i]] = i;
this->n = n;
newnode();
}
void insert(char *str,int root = 0){
for(int i = 0; str[i]; ++i){
int &x = ch[root][mp[str[i]]];
if(!x) x = newnode();
root = ch[root][mp[str[i]]];
}
++cnt[root];
}
void build(int root = 0){
queue<int>q;
for(int i = 0; i < n; ++i)
if(ch[root][i]) q.push(ch[root][i]);
while(!q.empty()){
root = q.front();
q.pop();
cnt[root] += cnt[fail[root]];
for(int i = 0; i < n; ++i){
int &x = ch[root][i],y = ch[fail[root]][i];
if(x){
fail[x] = y;
q.push(x);
}else x = y;
}
}
}
void solve(int m){
for(int i = 0; i < 110; ++i)
for(int j = 0; j < 110; ++j)
dp[i][j] = BigInt(0);
dp[0][0] = BigInt(1);
for(int i = 1; i <= m; ++i){
for(int j = 0;j < tot; ++j){
if(cnt[j]) continue;
for(int k = 0; k < n; ++k){
int x = ch[j][k];
if(cnt[x]) continue;
dp[i][x] = dp[i][x] + dp[i-1][j];
}
}
}
BigInt ans = BigInt(0);
for(int i = 0; i < tot; ++i)
ans = ans + dp[m][i];
ans.output();
}
}ac;
int main(){
int n,m,p;
char str[maxn];
while(~scanf("%d%d%d",&n,&m,&p)){
scanf("%s",str);
ac.init(str,n);
while(p--){
scanf("%s",str);
ac.insert(str);
}
ac.build();
ac.solve(m);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: