zoukankan      html  css  js  c++  java
  • uva 1401

    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 S 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
    Nanjing 2007-2008
    3942 - Remember

    dp[i] = sum(dp[i + len))

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 
     7 using namespace std;
     8 
     9 const int MAX = 3e5 + 7;
    10 const int MOD = 20071027;
    11 const int maxnode =  4000 * 100 + 8;
    12 int ch[maxnode][26];
    13 int val[maxnode];
    14 int sz;
    15 int idx(char c) { return c - 'a';}
    16 
    17 void insert(char *s, int v) {
    18     int u = 0, n = strlen(s);
    19     for (int i = 0; i < n ; ++i) {
    20         int c = idx(s[i]);
    21         if (!ch[u][c]) {
    22             memset(ch[sz], 0, sizeof(ch[sz]));
    23             val[sz] = 0;
    24             ch[u][c] = sz++;
    25         }
    26         u = ch[u][c];
    27     }
    28 
    29     val[u] = v;
    30 }
    31 
    32 
    33 char str[MAX];
    34 int s;
    35 int dp[MAX];
    36 
    37 void solve(int ca) {
    38     memset(dp, 0, sizeof(dp));
    39     int n = strlen(str);
    40     dp[n] = 1;
    41     for (int i = n - 1; i >= 0; --i) {
    42         int u = 0;
    43         for (int j = i; j < n; ++j) {
    44             if (ch[u][ idx(str[j]) ]) {
    45                 if (val[ ch[u][ idx(str[j]) ] ] == 1) dp[i] = (dp[i] + dp[j + 1]) % MOD;
    46                 u = ch[u][ idx(str[j]) ];
    47             } else {
    48                 break;
    49             }
    50         }
    51     }
    52 
    53     printf("Case %d: %d
    ", ca, dp[0]);
    54 }
    55 
    56 int main()
    57 {
    58    // freopen("sw.in", "r", stdin);
    59     int ca = 1;
    60     while (scanf("%s", str) != EOF) {
    61         scanf("%d", &s);
    62         sz = 1;
    63         memset(ch[0], 0, sizeof(ch[0]));
    64         char ss[105];
    65         for (int i = 0; i < s; ++i) {
    66             scanf("%s", ss);
    67             insert(ss, 1);
    68         }
    69 
    70         solve(ca++);
    71 
    72 
    73     }
    74     //cout << "Hello world!" << endl;
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    访问者模式
    oracle触发器简单实用示例
    C#控件交互效果类(也可以用作缩小面板放大,展示更多信息)
    23种设计模式探索C#
    windows快捷操作个人记录(常用)
    C#巧妙使用关键字async/await
    学习的枚举类型,结构以及初步了解数组
    目前学习.net时间让我摸不着头脑的事情
    对C#中几个循环语句的使用,请教
    学习了用控制台显示结果
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3910226.html
Copyright © 2011-2022 走看看