zoukankan      html  css  js  c++  java
  • HDU-2243

    考研路茫茫——单词情结

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4179    Accepted Submission(s): 1225


    Problem Description
    背单词,始终是复习英语的重要环节。在荒废了3年大学生涯后,Lele也终于要开始背单词了。
    一天,Lele在某本单词书上看到了一个根据词根来背单词的方法。比如"ab",放在单词前一般表示"相反,变坏,离去"等。

    于是Lele想,如果背了N个词根,那这些词根到底会不会在单词里出现呢。更确切的描述是:长度不超过L,只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个呢?这里就不考虑单词是否有实际意义。

    比如一共有2个词根 aa 和 ab ,则可能存在104个长度不超过3的单词,分别为
    (2个) aa,ab,
    (26个)aaa,aab,aac...aaz,
    (26个)aba,abb,abc...abz,
    (25个)baa,caa,daa...zaa,
    (25个)bab,cab,dab...zab。

    这个只是很小的情况。而对于其他复杂点的情况,Lele实在是数不出来了,现在就请你帮帮他。
     
    Input
    本题目包含多组数据,请处理到文件结束。
    每组数据占两行。
    第一行有两个正整数N和L。(0<N<6,0<L<2^31)
    第二行有N个词根,每个词根仅由小写字母组成,长度不超过5。两个词根中间用一个空格分隔开。
     
    Output
    对于每组数据,请在一行里输出一共可能的单词数目。
    由于结果可能非常巨大,你只需要输出单词总数模2^64的值。
     
    Sample Input
    2 3
    aa ab
    1 2
    a
     
    Sample Output
    104 52
     
    Author
    linle
     
    Recommend
    lcy
    /**
        题意:给出n个字符串,问长度为1~m的字符串中有多少是包含这n个字符串的
        做法:AC自动机 + 矩阵快速幂 长度为1~m的字符串中有 pow(26.0,1) + ..... + pow(26.0,m)种
             然后不包含病毒的有quick_pow(Maxtrix a,m);
             所以包含病毒的有 pow(26.0,1) + ..... + pow(26.0,m) - quick_pow(Maxtrix a,m)种
    **/
    #include <iostream>
    #include <cmath>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <queue>
    #include <map>
    #define MM 10
    using namespace std;
    struct Matrix
    {
        unsigned long long mat[140][140];
        int n;
        Matrix() {}
        Matrix(int _n)
        {
            n = _n;
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    mat[i][j] = 0;
                }
            }
        }
        Matrix operator *(const Matrix &b) const
        {
            Matrix res = Matrix(n);
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<n; j++)
                {
                    res.mat[i][j] = 0;
                    for(int k=0; k<n; k++)
                    {
                        res.mat[i][j] += mat[i][k] * b.mat[k][j];
                    }
                }
            }
            return res;
        }
    };
    unsigned long long quick_pow(unsigned long long a,int n)
    {
        unsigned long long res = 1;
        unsigned long long tmp = a;
        while(n)
        {
            if(n&1) res *= tmp;
            tmp *= tmp;
            n >>= 1;
        }
        return res;
    }
    Matrix quick_pow(Matrix a,int n)
    {
        Matrix res = Matrix(a.n);
        for(int i=0; i<a.n; i++)
        {
            res.mat[i][i] = 1;
        }
        Matrix tmp = a;
        while(n)
        {
            if(n&1) res =res * tmp;
            tmp = tmp * tmp;
            n >>= 1;
        }
        return res;
    }
    struct Tire
    {
        int next[110][26],fail[110];
        bool end[110];
        int L,root;
        int newnode()
        {
            for(int i=0; i<26; i++)
            {
                next[L][i] = -1;
            }
            end[L++] = 0;
            return L-1;
        }
        void init()
        {
            L = 0;
            root = newnode();
        }
        void insert(char buf[])
        {
            int now = root;
            int len = strlen(buf);
            for(int i=0; i<len; i++)
            {
                if(next[now][buf[i]-'a'] == -1)
                    next[now][buf[i]-'a'] = newnode();
                now = next[now][buf[i]-'a'] ;
            }
            end[now] = true;
        }
        void build()
        {
            queue<int>que;
            int now = root;
            fail[root] = root;
            for(int i=0; i<26; i++)
            {
                if(next[now][i] == -1)
                    next[now][i] = root;
                else
                {
                    fail[next[now][i]] = root;
                    que.push(next[now][i]);
                }
            }
            while(!que.empty())
            {
                now = que.front();
                que.pop();
                if(end[fail[now]]) end[now] = true;
                for(int i=0; i<26; i++)
                {
                    if(next[now][i] == -1)
                        next[now][i] = next[fail[now]][i];
                    else
                    {
                        fail[next[now][i]] = next[fail[now]][i];
                        que.push(next[now][i]);
                    }
                }
            }
        }
        Matrix getMatrix()
        {
            Matrix res = Matrix(L+1);
            for(int i=0; i<L; i++)
            {
                for(int j=0; j<26; j++)
                {
                    if(end[next[i][j]] == false && !end[i])
                        res.mat[i][next[i][j]] ++;
                }
            }
            for(int i = 0; i < L+1; i++)
                res.mat[i][L] = 1;
            return res;
        }
    };
    char buf[100];
    Tire ac;
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,m;
        while(~scanf("%d %d",&n,&m))
        {
            ac.init();
            for(int i=0; i<n; i++)
            {
                scanf("%s",buf);
                ac.insert(buf);
            }
            ac.build();
            Matrix a = ac.getMatrix();
            a = quick_pow(a,m);
            unsigned long long res = 0;
            for(int i=0; i<a.n; i++)
            {
                res += a.mat[0][i];
            }
            res--;
            unsigned long long sum = 0;
            Matrix c = Matrix(2);
            c.mat[0][0] = 26;
            c.mat[1][0] = c.mat[1][1]= 1;
            c = quick_pow(c,m);
            sum = c.mat[1][0] + c.mat[0][0];
            sum --;
            cout<<sum-res<<endl;
        }
        return 0;
    }
  • 相关阅读:
    excel导入数据库表
    C# WinForm通过WebClient实现文件上传下载
    C#中的多线程——线程同步基础
    document.body.scrollTop为0的处理办法
    C#利用短信猫收发短信息的方法
    XML Serializable Generic Dictionary
    Making IE use PNG Alpha transparency
    String[3]: the Size property has an invalid size of 0.
    input style兼容IE6的方案
    安装window service 中出现Set Service Login对话框
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4487349.html
Copyright © 2011-2022 走看看