1401 - Remember the Word

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

 

 

DP,设d[i]为从下标i开始的串的能分的种树,则d[i] = sum{d[i...L(w)] | W是所有以i下标字母开头的单词,L是W的长度};

这道题,提交了多遍,老是RE,最后差得结果是在递归中开了内存,当数据很大时,则内存溢出了。一定注意不要在递归中随便开新的内存,谨慎使用list, new.

还有一个问题,就是初始化,这是我敲代码常出错的地方。

 

 

 

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>

using namespace std;

const int MAX_LEN = 300000 + 10, MAX_SIZE = 4000 * 100 + 10,MAX_WORD = 100 + 5;
const int MAX_CHR = 26 + 4,MOD = 20071027;

char sq[MAX_LEN];
int chd[MAX_SIZE][MAX_CHR],val[MAX_SIZE],size,s;
int d[MAX_LEN];

int idx(char c)
{
    return c - 'a';
}

void insert(char *s)
{
    int cur = 0;
    for(int i = 0; s[i]; ++i)
    {
        int c = idx(s[i]);
        if(!chd[cur][c])
        {
            chd[cur][c] = size;
            val[size]  = 0;
            cur = size;
            size++;
        }
        else cur = chd[cur][c];
    val[cur] = 1;
}

void get_words_len(char *s ,int &cur_len)
{
    int cur = 0,pos = 0;
    for(int i = 0; s[i]; ++i)
    {
        int c = idx(s[i]);
        if(!chd[cur][c])break;

        cur = chd[cur][c];
        if(val[cur] && (i + 1) > cur_len)
        {
            cur_len = i + 1;
            return;
        }
    }

    cur_len = 0;
}


int calcu(char *str)
{
    if(str[0] == 0)return 1;
    int idx = str - sq;
    if(d[idx]!= -1) return d[idx];


    int words_len = 0;
    while(1)
    {
        get_words_len(str, words_len);
        if(!words_len)break;
        int val;
        val = calcu(str + words_len);
        d[idx] = d[idx] == -1 ? val : d[idx] + val;
        d[idx] %= MOD;
    }

    if(d[idx] == -1)d[idx] = 0;
    return d[idx];
}

int main()
{
    char word[MAX_WORD];
    int cnt = 0;
    while(scanf("%s",sq) != -1)
    {
        scanf("%d",&s);
        size = 1;
        
        memset(chd, 0, sizeof(chd[0]) * (s + 5) * 100);
        memset(val, 0, sizeof(int) * (s + 5) * 100);
        int len = strlen(sq);
        for(int i = 0; i <= len; ++i)d[i] = -1;

        while(s--)
        {
            scanf("%s",word);
            insert(word);
        }

        printf("Case %d: %d\n",++cnt, calcu(sq));
    }
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table: Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indi- cated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtaposi- tions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.” Write a C++ program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值