• POJ 1625 Censored!


    辣鸡OI毁我青春

    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
    

    Source

    Northeastern Europe 2001, Northern Subregion

    把P串建成AC自动机,在trie树上动规。f[i][j]表示字符串长度为i,目前走到ac自动机的j结点时的可行方案数。

    循环i,j,枚举尝试j可到的下一个结点k,若自动机上k结点不是终止结点,则可以转移:f[i+1][k]+=f[i][j]

    P串中可能会出现一个包含另一个的情况。处理方法:在建树时,若fail[x]是终止结点,x也标记成终止结点。

    加了个map映射来缩小树规模,其实直接用普通int数组映射也行

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstdio>
      4 #include<cmath>
      5 #include<cstring>
      6 #include<queue>
      7 #include<map>
      8 using namespace std;
      9 int n,m,p;
     10 char alpha[60];
     11 map<char,int> mp;
     12 struct num
     13 {
     14     int l,a[100];
     15     num operator + (const num &x) const
     16     {
     17         num ans;
     18         int len;
     19         memset(ans.a,0,sizeof(ans.a));
     20         for (int i=1;i<=l||i<=x.l;i++)
     21         {
     22             ans.a[i]+=a[i]+x.a[i];
     23             ans.a[i+1]+=ans.a[i]/10000;
     24             ans.a[i]%=10000;
     25         }
     26         if (l<x.l) len=x.l+1;
     27         else len=l+1;
     28         while (!ans.a[len]&&len) len--;
     29         ans.l=len;
     30         return ans;
     31     }
     32 }f[110][110],ans;
     33 void prt(num x)
     34 {
     35     printf("%d",x.a[x.l]);
     36     for (int i=x.l-1;i>=1;i--)
     37     {
     38         int y=x.a[i];
     39         if (y<1000) printf("0");            
     40         if (y<100) printf("0");
     41         if (y<10) printf("0");
     42         printf("%d",y);
     43     }
     44 }
     45 struct ACa
     46 {
     47     int next[500][100];
     48     int fail[500],end[500];
     49     int root,cnt;//根结点,计数器 
     50     int newnode(){
     51         for(int i=0;i<n;i++){
     52             next[cnt][i]=-1;//子结点设为空 
     53         }
     54         end[cnt++]=0;//以该结点结束的串数 
     55         return cnt-1; 
     56     }
     57     void clear(){//初始化 
     58         cnt=0;//结点数重置 
     59         root=newnode();//初始化根结点 //完成后root=0 
     60     }
     61     void insert(char c[]){
     62         int now=root;
     63         int len=strlen(c);
     64         for(int i=0;i<len;i++){
     65             if(next[now][mp[c[i]]]==-1)//如果对应结点未建立,添加结点 
     66                 next[now][mp[c[i]]]=newnode();
     67             now=next[now][mp[c[i]]];//否则沿结点继续 
     68         }
     69         end[now]++;
     70     }
     71     void build(){
     72         queue<int>q;
     73         fail[root]=root;
     74         for(int i=0;i<n;i++){
     75             if(next[root][i]==-1)
     76                 next[root][i]=root;
     77             else{//有结点则入队 
     78                 fail[next[root][i]]=root;
     79                 q.push(next[root][i]);
     80             }
     81             end[next[root][i]]|=end[next[root][i]];
     82         }
     83         while(!q.empty())
     84         {
     85             int now=q.front();
     86             q.pop();
     87             end[now]|=end[fail[now]];
     88             for(int i=0;i<n;i++){
     89                 if(next[now][i]==-1)
     90                   next[now][i]=next[fail[now]][i];//链接到fail指针对应结点的后面
     91                 else{
     92                   fail[next[now][i]]=next[fail[now]][i];
     93                   q.push(next[now][i]);
     94                 }
     95             }
     96         }
     97         return;
     98     }
     99 };
    100 ACa ac;
    101 int main(){
    102     scanf("%d%d%d
    ",&n,&m,&p);
    103     int i,j;int k;
    104     scanf("%s",alpha);
    105     for(i=0;i<n;i++)mp[alpha[i]]=i;//
    106     char c[60];
    107     ac.clear();
    108     for(i=1;i<=p;i++){
    109         scanf("%s",c);
    110         ac.insert(c);
    111     }
    112     ac.build();
    113     f[0][0].l=1;
    114     f[0][0].a[1]=1;
    115     for(i=0;i<m;i++){//字符串长度 
    116         for(j=0;j<ac.cnt;j++)//ac自动机的结点 
    117             for(k=0;k<n;k++){
    118                 int to=ac.next[j][k];//下一步尝试到达的结点
    119                 if(ac.end[to])continue;
    120                     f[i+1][to]=f[i+1][to]+f[i][j];
    121             }
    122     }
    123     for(i=0;i<ac.cnt;i++)if(!ac.end[i])ans=ans+f[m][i];
    124     prt(ans);
    125     return 0;
    126 }
  • 相关阅读:
    4.22课堂
    4.21课堂
    4.20作业
    4.20课堂
    4.17课堂
    4.16课堂
    4.15作业
    4.15反射与内置方法
    4.10绑定与非绑定
    70、django中间件
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5644170.html
Copyright © 2020-2023  润新知