[POJ 1625] Censored! (AC自动机+DP+大整数)
2015-11-16 19:50
253 查看
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
很普通的一道禁止字符串的题,但是就是死活不AC,和网络上的一些程序也对拍过了大数据,都没问题。想不明白啊……先放着吧。
已AC,大整数输出写挂了←_←
本题坑点:
(1)据说输入数据的字符中会有空格,所以请不要用scanf加%s读入,用gets。
(2)据说输入数据的字符可能会大于127(为负数),所以请不要用数组来映射,建议用map。
(3)这题空间有比较严的限制,所以10进制的大整数是行不通的。
数据可以去POJ的discuss找。
以下是一段蠢得不行的代码。
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
很普通的一道禁止字符串的题,但是就是死活不AC,和网络上的一些程序也对拍过了大数据,都没问题。想不明白啊……先放着吧。
已AC,大整数输出写挂了←_←
本题坑点:
(1)据说输入数据的字符中会有空格,所以请不要用scanf加%s读入,用gets。
(2)据说输入数据的字符可能会大于127(为负数),所以请不要用数组来映射,建议用map。
(3)这题空间有比较严的限制,所以10进制的大整数是行不通的。
数据可以去POJ的discuss找。
以下是一段蠢得不行的代码。
#include <cstdio> #include <cstdlib> #include <cstring> #include <map> using namespace std; const int bignlen = 60, MOD = 1000000, perlen = 6; struct trnode { char chr; int nxt[55], fail, last, val; }; struct bign { bign(){memset(bn, 0, sizeof(bn));} int bn[bignlen]; }; void addstr(char*); void calfail(); void bignprint(bign); bign operator + (bign a, bign b) { bign tem; for(int i=0; i<bignlen-1; i++) { tem.bn[i] += a.bn[i] + b.bn[i]; tem.bn[i+1] += tem.bn[i]/MOD; tem.bn[i]%=MOD; } return tem; }; int N,M,P; int chn[1000]; char alp[1000]; trnode trie[550]; int triep; int que[505]; bign dp[51][550]; bool vis[51][550]; map<char, int> mp; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(scanf("%d%d%d\n", &N, &M, &P ) != EOF) { triep = 0; memset(trie, 0, sizeof(trie)); memset(chn, 0, sizeof(chn)); memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis)); mp.clear(); gets(alp); for(int i=0; i<N; i++) mp[alp[i]] = i;//chn[alp[i]] = i; for(int i=0; i<P; i++) { char patn[20]; gets(patn); addstr(patn); } calfail(); dp[0][0].bn[0] = 1; vis[0][0] = 1; for(int i=0; i<M; i++) { for(int np=0; np<=triep; np++) { if(!vis[i][np]) continue; for(int k=0; k<N; k++) { int nxp = trie[np].nxt[k]; if(trie[nxp].val) continue; dp[i+1][nxp] = dp[i+1][nxp] + dp[i][np]; vis[i+1][nxp] = 1; } } } bign ans; for(int i=0; i<=triep; i++) ans = ans + dp[M][i]; bignprint(ans); } return 0; } void bignprint(bign x) { int len = bignlen-1; while(!x.bn[len] && len>-1) len--; if(len == -1) printf("0"); else { for(int i=len; i>-1; i--) { int tem = x.bn[i]; if(i != len) { int ilen = 0; while(tem) tem/=10, ilen++; for(int j=1; j<=perlen-ilen; j++) printf("0"); } if(x.bn[i]) printf("%d", x.bn[i]); //原来这里没有if,导致这一位全部为0的话,会多输出一个0 } } puts(""); return; } void addstr(char *patn) { int len = strlen(patn); int np = 0; for(int i=0; i<len; i++) { int chnum = mp[patn[i]];//chn[patn[i]]; int nxp = trie[np].nxt[chnum]; if(nxp) np = nxp; else { triep++; trie[np].nxt[chnum] = triep; np = triep; } if(i==len-1) trie[np].val = 1; } return; } void calfail() { int qhead = 0, qtail = 0; for(int i=0; i<N; i++) if(trie[0].nxt[i]) que[++qtail] = trie[0].nxt[i]; while(qhead<qtail) { int np = que[++qhead]; for(int i=0; i<N; i++) { int nxp = trie[np].nxt[i]; int fap = trie[np].fail; if(!nxp){ trie[np].nxt[i] = trie[fap].nxt[i]; continue;} que[++qtail] = nxp; trie[nxp].fail = trie[fap].nxt[i]; fap = trie[nxp].fail; if(trie[fap].val) trie[nxp].val = 1; } } return; }
相关文章推荐
- .pch文件的创建
- Unity与Android接口互相调用
- 一些重要的标准数组
- [LeetCode]89. Partition List链表划分
- c/c++内存释放知识总结
- ViewPager和视差动画
- 虚调用及其调用的具体形式
- 深入理解BootStrap Item12 -- 标签(label)与徽章(badge)
- 搞ACM的你伤不起(转)
- Java多线程安全问题
- 虚调用及其调用的具体形式
- TextView属性的静态使用与动态使用
- 使用Email发送功能
- PTAM图像接口需要灰度图像
- 武汉大学编译原理第4次作业
- ubuntu12.04连接winscp服务器时出现了:拒绝了SFTP连接,但它监听FTP连接。 想要用FTP协议来代替SFTP吗?最好是用加密的。
- CoreData 数组越界
- 安卓高级xml布局(一)高级输入框EditText设计
- c++利用链表写一个简单的栈
- DoIt is Being Flooded