LA3942 Remember the Word Trie+DP

题目给出了一个原始串,串长不超过300000

然后给出了n个单词,问用这个n个单词中的一些单词组成原始串一共有多少种组法,可以重复使用单词。

用dp(i)表示从 原始串 i 到 n一共有多少种组发

dp(i)=sum(dp(i+len(x)));

其中x是原始串s【i。。。n】的前缀。

查找前缀可以使用字典树。由于单词长度不超过100,因此每次最多只要比较100个节点即可,每当碰到单词节点,即可累加到dp[i]中。

但是如果不用字典树则最多要比较300000*4000次并且每一个单词的比较还会耗费时间。


Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

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


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>

using namespace std;

#define MAXN 4000*100+200
#define MAX 26

int sz,t[MAXN][MAX];
int jud[MAXN];

void clear(){
    sz=1;
    memset(t[0],-1,sizeof(t[0]));
    jud[0]=0;
}

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

void insert(char *s,int v){
    int u=0;
    int n=strlen(s);
    for(int i=0;i<n;i++){
        int c=idx(s[i]);
        if(t[u][c]==-1){
            memset(t[sz],-1,sizeof(t[sz]));
            jud[sz]=0;
            t[u][c]=sz++;
        }
        u=t[u][c];
    }
    jud[u]=v;
}

const int mod=20071027;
char s[200];
char str[300000+300];
int dp[300000+300];

void func(char *s,int pos){
    int u=0;
    int p=pos;
    for(;s[p]!='\0';p++){
        int c=idx(s[p]);
        if(t[u][c]==-1) return;
        u=t[u][c];
        if(jud[u])
            dp[pos]=(dp[pos]+dp[p+1])%mod;
    }
}

int main(){
    int cs=1;
    while(~scanf("%s",str)){
        int n;
        scanf("%d",&n);
        clear();
        for(int i=0;i<n;i++){
            scanf("%s",s);
            insert(s,1);
        }
        memset(dp,0,sizeof(dp));
        int sz=strlen(str);
        dp[sz]=1;
        for(int i=sz-1;i>=0;i--)
            func(str,i);
        printf("Case %d: %d\n",cs++,dp[0]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值