POJ 1625 Censored!
2015-11-05 14:58
369 查看
Censored!
Time Limit: 5000msMemory 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
相关文章推荐
- 12个有趣的C语言面试题
- 自定义绘制柱形图
- mybatis在xml文件中处理大于号小于号的方法
- 自己动手写RTP服务器——用RTP协议传输TS流
- 自定义绘制柱形图
- 自定义绘制柱形图
- Android开发总结笔记 ExpandableListView (折叠列表) 1-3-6
- C++ 矩阵加法与乘法的实现
- Firefox浏览器未安装在默认目录下,运行junit test case报错
- ziparchiver添加后编译出错
- MyBatis对多关系:显示该用户的所有角色
- Java随机生成手机短信验证码的方法
- 详解Swift编程中下标的用法
- PermGenspace错误解决方法java.lang.OutOfMemoryError:Perm
- Android Camera架构浅析
- 检索数据库中歌曲信息
- ip,pv,uv
- Deep Learning 7_深度学习UFLDL教程:Self-Taught Learning_Exercise(斯坦福大学深度学习教程)
- poj 2940 Wine Trading in Gergovia 贪心+扫描
- 你可以更幸福(转载)