• LA 3942 Remember the Word 字典树 dp


    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.

    Since Jiejie can't remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.

    The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.

    Input 

    The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.

    The second line contains an integer S , 1$ \le$S$ \le$4000 .

    Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.

    There is a blank line between consecutive test cases.

    You should proceed to the end of file.

    Output 

    For each test case, output the number, as described above, from the task description modulo 20071027.

    Sample Input 

    abcd 
    4 
    a 
    b 
    cd 
    ab
    

    Sample Output 

    Case 1: 2

    ------------------------------------------------------------
    给出一个由S个不同单词组成的字典和一个长字符串,把这个字符串分解成若干个单词的链接,单词可以重复使用,有多少种方法?
    f[i]表示以字符i为结尾的单词分解方法,若一个长度为x的单词与以i+1为开头的部分相匹配,则f[i+x]=f[i+x]+f[i];
    将单词建立一个字典树,用字典树快速求出匹配的单词。


    ------------------------------------------------------------
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    
    using namespace std;
    
    char str[333333];
    char words[4444][111];
    int f[333333];
    
    //用类,或者结构体定义都行
    struct trie
    {
        trie* next[26];
        int num;
        int value;
        trie()
        {
           for(int i=0;i<26;i++) next[i]=NULL;
           value=0;//记录是不是一个单词
           num=0;//记录单词出现的次数
        }
        void clear()
        {
           for(int i=0;i<26;i++) next[i]=NULL;
           value=0;//记录是不是一个单词
           num=0;//记录单词出现的次数
        }
    }root;
    
    //插入:
    void insert(char* s)
    {
        trie* p=&root;
        int k=0;
        while(s[k]!='\0')
        {
            if(!p->next[s[k]-'a']) p->next[s[k]-'a']=new trie;
            p=p->next[s[k]-'a'];
            p->num++;
            k++;
        }
        p->value=1;
    }
    
    //查找
    void find(char* s,int pos)
    {
        trie* p=&root;
        int k=0;
        while(s[k]!='\0'&&p->next[s[k]-'a'])
        {
            p=p->next[s[k]-'a'];
            if (p->value==1)
            {
                f[pos+k+1]=(f[pos+k+1]+f[pos])%20071027;
            }
            k++;
        }
    }
    
    int main()
    {
        int l,s;
        int cnt=0;
        while (~scanf("%s",str+1))
        {
            cnt++;
            memset(f,0,sizeof(f));
            root.clear();
            scanf("%d",&s);
            l=strlen(str+1);
            for (int i=0;i<s;i++)
            {
                scanf("%s",words[i]);
                insert(words[i]);
            }
            f[0]=1;
            for (int i=1;i<=l;i++)
            {
                if (f[i-1])
                {
                    find(str+i,i-1);
                }
            }
            printf("Case %d: %d\n",cnt,f[l]);
        }
        return 0;
    }
    








  • 相关阅读:
    游戏开发之路小结(二):关于第一人称射击游戏开发实战小结
    游戏开发之路小结(一):关于太空射击游戏开发实战小结
    游戏开发要涉及的几个方面
    短短几行代码实现让摄像机跟随着物体效果
    关于移动设备几种屏幕输入方式的小结
    软件的体系架构摘要
    jQuery入门笔记之(一)选择器引擎-【转】
    ASCII码对照表
    将数字转出大写如:100转换后结果为一佰
    google搜索技巧总结
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038415.html
Copyright © 2020-2023  润新知