LA3942 Remember the Word(字典树+DP)

题意:给你一串序列,再给一个n,再输入n个字符串,问这n个字符串随意组合有多少种组合可以变成原序列

思路:令dp[i]为从i到str末尾有多少种组合,那么dp[i]=sum(dp[k+1])(k>=i,且区间[i,k]所构成的字符串是存在在原串中),由于需要大量的判断某个字符串是否是原串的前缀,所以建一颗字典树就可以啦


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 300000+500
#define LL long long
#define mod 20071027
int cas=1,T;
struct Tree
{
	int L,root;
	int Next[maxn][27];
	int word[maxn];
	int val[maxn];
	void init()
	{
		L=0;
		root=newnode();
	}
	int newnode()
	{
		for (int i = 0;i<26;i++)
			Next[L][i]=-1;
		val[L]=0;
		word[L++]=0;
		return L-1;
	}
	void insert(char *s,int v)
	{
		int l = strlen(s);
		int u = root;
		for (int i = 0;i<l;i++)
		{
			int c = s[i]-'a';
	//		val[u]=0;
			if (Next[u][c]==-1)
				Next[u][c]=newnode();
			u=Next[u][c];
			word[u]++;
		//	val[u]=0;
		}
		val[u]=v;
	}
	int find(char *s)
	{
		int u = root;
		int l = strlen(s);
		for (int i = 0;i<l;i++)
		{
			int c = s[i]-'a';
			if (Next[u][c]==-1)
				return false;
			u=Next[u][c];
		
		}
		return val[u];
	}
}tree;
char str[maxn];
char temp[101];
LL dp[maxn];
int main()
{
	while (scanf("%s",str)!=EOF)
	{
		int n;
		scanf("%d",&n);
        tree.init();
		for (int i = 0;i<n;i++)
		{
			scanf("%s",temp);
			tree.insert(temp,1);
		}
		int m = strlen(str);
		memset(dp,0,sizeof(dp));
		dp[m]=1;
		for (int i = m-1;i>=0;i--)
		{
			int u = 0;
			for (int j = i;j<m;j++)
			{
				int c = str[j]-'a';
				if (tree.Next[u][c]==-1)
					break;
				u=tree.Next[u][c];
				if (tree.val[u])
					dp[i]+=dp[j+1];
			}
			dp[i]%=mod;
		}
		printf("Case %d: %lld\n",cas++,dp[0]);
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值