博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ1625]Censored!(AC自动机+DP+高精度)
阅读量:4883 次
发布时间:2019-06-11

本文共 3690 字,大约阅读时间需要 12 分钟。

 

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

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 1abbb

Sample Output

5

Source

, Northern Subregion

同HDU2222,只是需要高精度

感觉可能有个问题,就是这题题目没有规定屏蔽词包含了所有字母,但是好像对解题没有什么影响。

代码用时:1.5h 高精度输出写炸了

1 #include
2 #include
3 #include
4 #define rep(i,l,r) for (int i=l; i<=r; i++) 5 using namespace std; 6 7 const int N=110; 8 int n,m,p,nd,fail[N],b[N],q[N],c[N][N]; 9 char s[N],S[N];10 11 int get(char ch){ rep(i,1,n) if (S[i]==ch) return i; return -1; }12 13 void ins(char S[]){14 int x=0,len=strlen(s+1);15 rep(i,1,len){16 int k=get(s[i]);17 if (!c[x][k]) ++nd,c[x][k]=nd;18 x=c[x][k];19 }20 b[x]=1;21 }22 23 void getfail(){24 int st=0,ed=0;25 rep(i,1,n) if (c[0][i]) q[++ed]=c[0][i];26 while (st!=ed){27 int x=q[++st];28 rep(i,1,n)29 if (!c[x][i]) c[x][i]=c[fail[x]][i];30 else q[++ed]=c[x][i],fail[c[x][i]]=c[fail[x]][i];31 b[x]|=b[fail[x]];32 }33 }34 35 struct D{36 int v[N],len;37 D(int x=0){38 memset(v,0,sizeof(v));39 for (len=0; x>0; x/=10) v[len++]=x%10;40 len--;41 }42 D operator +(const D &a){43 D ans;44 ans.len=max(len,a.len);45 for (int i=0; i<=ans.len; i++){46 ans.v[i]+=v[i]+a.v[i]; ans.v[i+1]+=ans.v[i]/10; ans.v[i]%=10;47 }48 while (ans.v[ans.len+1]) ans.len++;49 return ans;50 }51 void print(){52 if (len==-1) { printf("%d\n",0); return; }53 for (int i=len; ~i; i--) printf("%d",v[i]);54 printf("\n");55 }56 }dp[52][N];57 58 int check(int k,int j){ int ans=0; rep(i,1,n) if (c[k][i]==j) ans++; return ans; }59 60 int main(){61 freopen("poj1625.in","r",stdin);62 freopen("poj1625.out","w",stdout);63 scanf("%d%d%d",&n,&m,&p); scanf("%s",S+1);64 rep(i,1,p) scanf("%s",s+1),ins(s);65 getfail(); dp[0][0]=D(1);66 rep(i,1,m) rep(j,0,nd) if (!b[j])67 rep(k,0,nd) if (!b[k])68 for (int ti=check(k,j); ti--; ) dp[i][j]=dp[i][j]+dp[i-1][k];69 D ans; rep(i,0,nd) ans=ans+dp[m][i];70 ans.print();71 return 0;72 }

 

转载于:https://www.cnblogs.com/HocRiser/p/8412943.html

你可能感兴趣的文章
linux不开启图形界面
查看>>
菜鸟学习SSH(二)——Struts国际化
查看>>
iOS 自定义控件--重写一些方法
查看>>
第二次冲刺作业
查看>>
【转】HTML, CSS和Javascript调试入门
查看>>
折线图-小案例
查看>>
STL:优先队列Priority Aueue
查看>>
蓝桥历年试题 套娃
查看>>
EF4.0和EF5.0增删改查的写法区别及执行Sql的方法
查看>>
作业一
查看>>
微信支付体验
查看>>
Excel导数据到数据库
查看>>
zz 悲催的程序员,以及程序员的悲催
查看>>
Thinkphp 3.2笔记
查看>>
RHEL7开机不能正常进入系统(图形化界面)
查看>>
Android开发环境搭建完全图解
查看>>
详解BOM头以及去掉BOM头的方法
查看>>
PHP 手机浏览器访问网站获取手机相关信息方法集锦
查看>>
09年电子竞赛参赛技巧经验11条(转载)
查看>>
CSS颜色
查看>>