Trie字典树+DP

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 ≤ 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

题目大意

给出一个由S个不同单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法?比如,有4个单词a,b,cd,ab,则abcd有2种分解方法:a+b+cd和ab+cd
输入:
输入包含多组数据。每组数据第一行为小写字母组成的待分解字符串,长度L不超过300000。第二行为单词个数S(S<=4000)。以下S行每行为一个单词,由不超过100个小写字母组成。输入结束的标志为文件结束符(EOF)

先用给定的每个单词建立一个字典树,然后用这个建立的字典树dp第一行的text文本;

具体的dp过程:记S为第一行的text字符串从后向前dp,考虑第i个位置(1<=i<=length),对于字符串S[i~length]应当在字典树中检索以S[i]开始的所有word(不超出text界限的范围内),假设word的长度为wlength,那么dp[i]+=dp[i+wlength],(dp[i]初始值为0),找完所以以S[i]开头的word并更新之后,dp[i]完成。从后向前一直更新,直到dp[0],最后输出就行;

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int tlength=3e5+10;
const int wlength=4e3+10;
const int maxn=4e5+4000;//这里因为记录长度的时候要占领(浪费)一个,所以加了4000 
const int mod=20071027;
int trie[maxn][26],cnt,lentag[maxn],s;//trie字典树,cnt是字典树的节点总和,lengtag记录word长度 
char word[wlength],text[tlength];
ll dp[tlength];
void buildtrie(char *w)//建立字典树,是一个动态开点的过程,只不过这里的一个点就是一行长度为26的数组 
{
	int u=0,wlen,c;//u当前节点,wlen是word长度 
	wlen=strlen(w);
	for(int i=0;i<wlen;i++)
	{
		c=w[i]-'a';
		if(trie[u][c]==0)// 如果该点未开,那么开点 
		trie[u][c]=++cnt;
		u=trie[u][c];//走到当前字符指向的点 
	}
	lentag[u]=wlen;//这里记录长度的时候相当于占用(浪费)了一个点的空间 
}
void solve(char *text)//对text进行dp 
{
	int tl,c,u;
	tl=strlen(text);
	dp[tl]=1;//这里dp[tl]已经是在字符串之外了 
	for(int i=tl-1;i>=0;i--)
	{
		u=0;
		for(int j=i;j<tl;j++)//如果超过了text范围就跳出,不再更新dp[i],主要是保证在更新靠近末尾的dp的时候不出错 
		{
			c=text[j]-'a';
			if(trie[u][c]==0)//字典树里找不到以text[i]开头的word了,说明dp[i]已经无法更新了 
			break;
			if(lentag[trie[u][c]])
			dp[i]=(dp[i]+dp[i+lentag[trie[u][c]]])%mod;//转移方程,记得取模 
			u=trie[u][c];
		}
	}
}
int main()
{
    int k=1;
	while(scanf("%s",text)!=EOF)
	{
		
		memset(trie,0,sizeof(trie));//记得每次清除一下 
		memset(lentag,0,sizeof(lentag));
		memset(dp,0,sizeof(dp));
		cnt=0;
		scanf(" %d",&s);
		for(int i=1;i<=s;i++)
		{
			scanf("%s",word);
			buildtrie(word);
		}
		solve(text);
		printf("Case %d: %lld\n",k,dp[0]);
		k++;
	}
    return 0;
}

2021.8.3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值