POJ1625-Censored!
来源:互联网 发布:编写sql语句的工具 编辑:程序博客网 时间:2024/06/07 12:07
Censored!
Time Limit: 5000MS Memory Limit: 10000KTotal Submissions: 10565 Accepted: 2891
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.
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.
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 1abbb
Sample Output
5
Source
Northeastern Europe 2001, Northern Subregion
题意:给你m个字符和p个字符串,问用这m个字符去构造一个长度为n的且不包含这p个字符串中任意一个的字符串能有几种
解题思路:ac自动机+dp,这个答案会很大,要用大数,所以矩阵快速幂不好弄,用dp做,dp[i][j]表示长度为i,在节点j满足条件的方案数
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cctype>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;struct BigInt{const static int mod = 10000;const static int DLEN = 4;int a[600], 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(const char s[]){memset(a, 0, sizeof(a));int L = strlen(s);len = L / DLEN;if (L%DLEN)len++;int index = 0;for (int i = L - 1; i >= 0; i -= DLEN){int t = 0;int k = i - DLEN + 1;if (k < 0)k = 0;for (int j = k; j <= i; j++)t = t * 10 + s[j] - '0';a[index++] = t;}}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;}BigInt operator *(const BigInt &b)const{BigInt res;for (int i = 0; i < len; i++){int up = 0;for (int j = 0; j < b.len; j++){int temp = a[i] * b.a[j] + res.a[i + j] + up;res.a[i + j] = temp%mod;up = temp / mod;}if (up != 0)res.a[i + b.len] = up;}res.len = len + b.len;while (res.a[res.len - 1] == 0 && res.len > 1)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[2][110];struct Matrix{int v[110][110];Matrix(){memset(v, 0, sizeof v);}}x;int n, m, p;char ch[100];map<char, int>mp;struct Trie{int next[110][60], fail[110], flag[110];int root, tot;int newnode(){for (int i = 0; i < n; i++) next[tot][i] = -1;flag[tot++] = 0;return tot - 1;}void init(){tot = 0;root = newnode();}void insert(char ch[]){int k = root;for (int i = 0; ch[i]; i++){if (next[k][mp[ch[i]]] == -1) next[k][mp[ch[i]]] = newnode();k = next[k][mp[ch[i]]];}flag[k] = 1;}void build(){queue<int>q;fail[root] = root;for (int i = 0; i < n; i++){if (next[root][i] == -1) next[root][i] = root;else{fail[next[root][i]] = root;q.push(next[root][i]);}}while (!q.empty()){int pre = q.front();q.pop();if (flag[fail[pre]]) flag[pre] = 1;for (int i = 0; i < n; i++){if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];else{fail[next[pre][i]] = next[fail[pre]][i];q.push(next[pre][i]);}}}}void solve(){for (int i = 0; i < tot; i++)for (int j = 0; j < n; j++)if (!flag[next[i][j]]) x.v[i][next[i][j]]++;int now = 0;for (int i = 0; i < tot; i++) dp[now][i] = 0;dp[now][0] = 1;for (int i = 1; i <= m; i++){for (int j = 0; j < tot; j++) dp[now ^ 1][j] = 0;for (int j = 0; j < tot; j++)for (int k = 0; k < tot; k++)if (x.v[j][k]) dp[now ^ 1][k] = dp[now ^ 1][k] + dp[now][j] * x.v[j][k];now ^= 1;}BigInt ans = 0;for (int i = 0; i < tot; i++) ans = ans + dp[now][i];ans.output();}void debug(){for (int i = 0; i < tot; i++){printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);printf("]\n");}}}ac;int main(){while (~scanf("%d %d %d", &n, &m, &p)){mp.clear();scanf("%s", ch);for (int i = 0; ch[i]; i++) mp[ch[i]] = i;ac.init();for (int i = 1; i <= p; i++){scanf("%s", &ch);ac.insert(ch);}ac.build();ac.solve();}return 0;}
阅读全文
0 0
- poj1625 Censored!
- POJ1625 Censored!
- POJ1625-Censored!
- [poj1625] 检查Censored!
- poj1625 Censored! AC自动机+DP
- POJ1625 Censored! AC自动机+DP+高精度
- poj1625 Censored! AC自动机DP、高精度
- poj1625 Censored! 高精度+ac机+dp
- POJ1625---Censored!(AC自动机+dp+高精度)
- POJ1625 Censored! AC自动机+dp+高精度
- poj1625
- poj1625
- POJ1625----AC自动机+大数+DP
- POJ1625--AC自动机+DP+大数
- AC自动机+DP+大数poj1625
- AC自动机+dp+大数 poj1625
- POJ1625-(AC自动机+DP+大数)
- poj 1625 Censored!
- Redis的数据类型简介
- 5.4已知银行不同期限存款的年息利率,求其本利之和
- LintCode-【容易】9.Fizz Buzz问题
- 树莓派3B+ 安装计算机视觉库(OpenCV_2.4.9官方源)
- Java for Web学习笔记(九三):消息和集群(8)RabbitMQ和消息模式(中)
- POJ1625-Censored!
- VINS mono 系统学习 五
- 最小生成树之安慰奶牛
- 带你彻底搞定希尔排序是个什么情况
- 声明和定义的区别
- 分治法计算乘幂
- 习题6.2(2)计算1*2*3+3*4*5+...+99*100*101的值
- LeetCode.720 Longest Word in Dictionary
- 讲一下Markdown