LA3942 Remember the world

 

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
 
转载请注明出处:http://blog.csdn.net/scut_pein/article/details/19681339
题意:给出一个由S个不同的单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接,有多少种方法?比如,有4个单词a,b,cd,ab,则abcd有两种分解方法:a+b+cd 和 ab+cd。
思路:最先想到的是记忆化搜索,dp[i]记录从i开始的分解种数,首先将单词插入trie,然后将母本在trie上跑,跑到标记为尾结点处则ans += dp[i+1]。(如果dp[i+1]没记录得先调用函数)。显然华丽丽地超时了、、、
然后想到从右往左递推,枚举长度1到100.然后在trie树上跑,如果能够跑到有标记的地方,则dp[i] += dp[i+j](j是枚举的长度)
注意初始化dp[len] = 1;
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 400080
char T[maxn],str[4080];
int dp[maxn];
#define MOD 20071027
struct Trie
{
	int ch[maxn][26];
	bool vis[maxn];
	int cnt;
	void init()
	{
		memset(ch[0],0,sizeof(ch[0]));
		memset(vis,0,sizeof(vis));
		cnt = 1;
	}
	
	int idx(char c)
	{
		return c - 'a';
	}
	
	void insert(char * s)
	{
		int len = strlen(s);
		int u = 0;
		for(int i = 0;i < len;i++)
		{
			int c = idx(s[i]);
			if(!ch[u][c])
			{
				memset(ch[cnt],0,sizeof(ch[cnt]));
				ch[u][c] = cnt++;
			}
			u = ch[u][c];
		}
		vis[u] = 1;
	}

	bool find(int pos,int len)
	{
		int u = 0;
		for(int i = pos;i < pos+len;i++)
		{
			int c = idx(T[i]);
			if(!ch[u][c])	return 0;
			u = ch[u][c];
		}
		if(vis[u])	return 1;
		return 0;
	}
	

}trie;


int main()
{
	//freopen("in.txt","r",stdin);
	int cas = 0;
	while(scanf("%s",T)!=EOF)
	{
		cas++;
		int n;
		scanf("%d",&n);
		trie.init();
		while(n--)
		{
			scanf("%s",&str);
			trie.insert(str);
		}
		memset(dp,0,sizeof(dp));
		int len = strlen(T);
		dp[len] = 1;
		for(int i = len-1;i >= 0;i--)
		{
			//枚举1到100看能否在树上找到有末尾标记的结点
			for(int j = 1;j <= 100;j++)
			{
				if(trie.find(i,j))
				{
					dp[i] += dp[i+j];
					if(dp[i] >= MOD)	dp[i] %= MOD;
				}
			}
		}
		printf("Case %d: %d\n",cas,dp[0]);
	}
	return 0;
}

 

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值