zoukankan      html  css  js  c++  java
  • POJ 1625 Censored!

    Censored!

    Time Limit: 5000ms
    Memory 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

     
    解题:Trie图 + 动态规划
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <queue>
      5 using namespace std;
      6 struct BigInt {
      7     const static int mod=10000;
      8     const static int dlen=4;
      9     int a[110],len;
     10 
     11     BigInt() {
     12         memset(a,0,sizeof(a));
     13         len=1;
     14     }
     15 
     16     BigInt(int v) {
     17         memset(a,0,sizeof(a));
     18         len=0;
     19         do {
     20             a[len++]=v%mod;
     21             v/=mod;
     22         } while(v);
     23     }
     24 
     25     BigInt operator + (const BigInt &b) const {
     26         BigInt res;
     27         res.len=max(len,b.len);
     28         for(int i=0; i<=res.len; i++) {
     29             res.a[i]=0;
     30         }
     31         for(int i=0; i<res.len; i++) {
     32             res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
     33             res.a[i+1]+=res.a[i]/mod;
     34             res.a[i]%=mod;
     35         }
     36         if(res.a[res.len]>0) res.len++;
     37         return res;
     38     }
     39 
     40     void output() {
     41         printf("%d",a[len-1]);
     42         for(int i=len-2; i>=0; i--)
     43             printf("%04d",a[i]);
     44         printf("
    ");
     45     }
     46 } dp[110][110];
     47 const int maxn = 256;
     48 struct Trie{
     49     int ch[maxn][maxn],fail[maxn],cnt[maxn],tot,n;
     50     int mp[256];
     51     int newnode(){
     52         memset(ch[tot],0,sizeof ch[tot]);
     53         fail[tot] = cnt[tot] = 0;
     54         return tot++;
     55     }
     56     void init(char *str,int n){
     57         tot = 0;
     58         for(int i = 0; i < n ; ++i) mp[str[i]] = i;
     59         this->n = n;
     60         newnode();
     61     }
     62     void insert(char *str,int root = 0){
     63         for(int i = 0; str[i]; ++i){
     64             int &x = ch[root][mp[str[i]]];
     65             if(!x) x = newnode();
     66             root = ch[root][mp[str[i]]];
     67         }
     68         ++cnt[root];
     69     }
     70     void build(int root = 0){
     71         queue<int>q;
     72         for(int i = 0; i < n; ++i)
     73             if(ch[root][i]) q.push(ch[root][i]);
     74         while(!q.empty()){
     75             root = q.front();
     76             q.pop();
     77             cnt[root] += cnt[fail[root]];
     78             for(int i = 0; i < n; ++i){
     79                 int &x = ch[root][i],y = ch[fail[root]][i];
     80                 if(x){
     81                     fail[x] = y;
     82                     q.push(x);
     83                 }else x = y;
     84             }
     85         }
     86     }
     87     void solve(int m){
     88         for(int i = 0; i < 110; ++i)
     89             for(int j = 0; j < 110; ++j)
     90                 dp[i][j] = BigInt(0);
     91         dp[0][0] = BigInt(1);
     92         for(int i = 1; i <= m; ++i){
     93             for(int j = 0;j < tot; ++j){
     94                 if(cnt[j]) continue;
     95                 for(int k = 0; k < n; ++k){
     96                     int x = ch[j][k];
     97                     if(cnt[x]) continue;
     98                     dp[i][x] = dp[i][x] + dp[i-1][j];
     99                 }
    100             }
    101         }
    102         BigInt ans = BigInt(0);
    103         for(int i = 0; i < tot; ++i)
    104             ans = ans + dp[m][i];
    105         ans.output();
    106     }
    107 }ac;
    108 int main(){
    109     int n,m,p;
    110     char str[maxn];
    111     while(~scanf("%d%d%d",&n,&m,&p)){
    112         scanf("%s",str);
    113         ac.init(str,n);
    114         while(p--){
    115             scanf("%s",str);
    116             ac.insert(str);
    117         }
    118         ac.build();
    119         ac.solve(m);
    120     }
    121     return 0;
    122 }
    View Code
  • 相关阅读:
    Qt刷新机制的一些总结(Qt内部画的时候是相当于画在后台一个对象里,然后在刷新的时候调用bitblt统一画,调用window的api并不会影响到后面的那个对象)
    delphi中formatFloat代码初探(在qt下实现floatformat的函数)
    Qt中如何写一个model(自定义一个RowNode,我没有碰到过)
    DNN简介以及安装
    理解C# 4 dynamic(4) – 让人惊艳的Clay
    C#基础知识系列八(const和readonly关键字)
    ASP.NET MVC 5 -从控制器访问数据模型
    随机数的算法
    c#拷贝
    MVC UnitOfWork EntityFramework架构
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4939548.html
Copyright © 2011-2022 走看看