【SCU - 3596 Article Decryption】 字典树 + DP

I - Article Decryption


Time Limit:1000ms Memory Limit:65536KB

Description

You are given an dictionary in which there are N words.
Also, you are given an article with the length of L and
there`s no punctuation.
How many way can you translate this article?

Input

The first line is an integer T which stands for the number of test cases.
Then T test cases follow.
The first line of a test case contains an integer N (1<=N<=1000).
Each of the following N lines contains one word . 
The length of each word is guaranteed to be less than or eaqul to 1000.
The next line contains some characters which describe the article. 
The characters are lowercase letters.

Output

For each case output its answer after mod by 835672545.

Sample Input

2
5
ab
cb
bc
ba
a
abcba
3
a
ba
ab
ababa

Sample Output

2
3

题意:给定n个字符串和一篇文章,问用这些字符串组成这篇文章的方法有多少种。


分析:先将给定的字符串存入字典树中,在树中定义一个cnt用来记录存入的每个字符串以最后一个字母结尾时的数量。这样我们可以定义一个数组dp[MX]来记录不同长度下的符合条件的组合方法。

dp[i] += dp[i-1] * query(s[ ])

query(s[ ])表示所查询的字符串的个数
最终答案就是dp[len] len为文章的长度

代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

#define mod 835672545
#define LL long long
#define INF 0x3f3f3f3f

using namespace std;
const int MX = 1e5 + 5;

int tot;
int dp[MX];
char temp[MX];
struct node{
    int nxt[26];
    int cnt;
    void init(){
        cnt = 0;
        memset(nxt, 0, sizeof(nxt));
    }
}tree[MX];

void add(char s[]){
    int now = 0, x;
    for(int i = 0; s[i] != '\0'; i++){
        x = s[i] - 'a';
        if(tree[now].nxt[x] == 0){
            tree[++tot].init();
            tree[now].nxt[x] = tot;
        }
        now = tree[now].nxt[x];
    }
    tree[now].cnt++;
}

int query(char s[]){
    int now = 0, x;
    for(int i = 0; s[i] != '\0'; i++){
        x = s[i] - 'a';
        if(tree[now].nxt[x]){
            now = tree[now].nxt[x];
        }
        else return 0;
    }
    return tree[now].cnt;
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        tot = 0;
        tree[0].init();
        int n;
        scanf("%d", &n);
        char s[1005];
        for(int i = 0; i < n; i++){
            scanf("%s", s);
            add(s);
        }
        scanf("%s", s+1);
        memset(dp, 0, sizeof(dp));
        memset(temp, 0, sizeof(temp));
        int len = strlen(s+1);
        dp[0] = 1;
        for(int i = 1; i <= len; i++){
            temp[i] = s[i];
            for(int j = 1; j <= i; j++){
                if(dp[j-1]){
                    dp[i] += dp[j-1] * query(temp + j);
                }
                if(dp[i] >= mod) dp[i] %= mod;
            }
        }
        printf("%d\n", dp[len]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值