zoukankan      html  css  js  c++  java
  • Remember the Word (UVA-1402)

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowingthat 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’sonly 20071027 sticks, he can only record the remainders of the numbers divided by total amount ofsticks.The problem is as follows: a word needs to be divided into small pieces in such a way that eachpiece is from some given set of words. Given a word and the set of words, Jiejie should calculate thenumber 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 wordwhose length is no more than 300 000.

    The second line contains an integer S, 1 ≤ S ≤ 4000.

    Each of the following S lines contains one word from the set. Each word will be at most 100characters 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.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

    这是一道很好的题目,(竟然是用到trie和dp~~).先把单词做成trie(字典树的形式),然后用动态规划,用dp[i]表示文本串第i个字符后面(既s[i....n])所形成的做多数量,然后推出转移方程dp[i]=dp[i]+dp[i+len[j]];

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<cstdio>
    using namespace std;


    const int maxnode = 4000 * 100 + 10;
    const int sigma_size = 26;
    const int maxl = 300000 + 10; 
    const int maxw = 4000 + 10;   
    const int maxwl = 100 + 10;   
    const int MOD = 20071027;


    int d[maxl], len[maxw], S;
    char text[maxl], word[maxwl];


    struct Trie {
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz; 
    void clear() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } 
    int idx(char c) { return c - 'a'; } 



    void insert(const char *s, int v) 
    {
    int u = 0, n = strlen(s);
    for (int i = 0; i < n; i++) 
    {
    int c = idx(s[i]);
    if (!ch[u][c]) 

    memset(ch[sz], 0, sizeof(ch[sz]));
    val[sz] = 0;  
    ch[u][c] = sz++; 
    }
    u = ch[u][c]; 
    }
    val[u] = v; 
    }



    void find(const char *s, int len, vector<int>& ans) {
    int u = 0;
    for (int i = 0; i < len; i++) 
    {
    int c = idx(s[i]);
    if (!ch[u][c]) break;
    u = ch[u][c];
    if (val[u]) ans.push_back(val[u]); 
    }
    }
    } trie;


    int main() 
    {
    int kase = 1;
    while (~scanf("%s%d",text,&S)) 
    {
    trie.clear();
    for (int i = 1; i <= S; i++) 
    {
    scanf("%s", word);
    len[i] = strlen(word);
    trie.insert(word, i);
    }
    memset(d, 0, sizeof(d));
    int L = strlen(text);
    d[L] = 1;
    for (int i = L - 1; i >= 0; i--) 
    {
    vector<int> p;
    trie.find(text + i, L - i, p);
    for (int j = 0; j < p.size(); j++)
    d[i] = (d[i] + d[i + len[p[j]]]) % MOD;
    }
    printf("Case %d: %d ", kase++, d[0]);
    }
    return 0;
    }







  • 相关阅读:
    (转)Android学习-使用Async-Http实现图片压缩并上传功能
    (转)Eclipse中需要查看某个类的源码,直接按住Ctrl 然后点击想要查看的类或则方法
    android如何判断控件的显示或者隐藏
    Android开发之利用ViewPager实现页面的切换(仿微信、QQ)
    Android开发之利用ViewPager实现在Activity或Fragment中引入别的布局文件实现滑动并进行页面跳转
    Android开发之自定义的ProgressDialog
    Android开发之SharedPreferences的封装
    Pytorch有什么节省显存的小技巧?
    pytorch模型提示超出内存cuda runtime error(2): out of memory
    Structured3D: A Large Photo-realistic Dataset for Structured 3D Modeling
  • 原文地址:https://www.cnblogs.com/csushl/p/9386576.html
Copyright © 2011-2022 走看看