Uva 1401 Remember the Word 字典树+DP

Neal is very curious about combinatorial problems, and now here comes a problem about words. Know-
ing 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


题意:给出一个字符串和一组单词,问用这些单词组成这个字符串有多少种不同的组合。

方法:字典树+DP

思路:首先考虑这个字符串,假设在这组单词中有一个是这个字符串的前缀,那么我们就可以不考虑这个前缀,直接考虑从前缀往后的字符串的组合方式。举个例子,对于abcd,单词中的a和ab都是它的前缀,假如是a,那么我们只需要知道bcd的组合方式即可;假如是ab,我们只需要知道cd的组合方式即可,也就是说这是一个逆推的DP,方程为dp[i]=sum{dp[i+len(x)|单词x是原始串的后缀S[i...L]的前缀},也就是说,我们必须从最后一个元素开始算,初始的时候,我们需要让dp[len]=1,因为假如一个前缀就是串本身,那么实际上只有一种组合方式。


#include <iostream>
#include <stdio.h>
#include <string.h>
#define MOD 20071027
using namespace std;

struct trie
{
	bool last;
	trie *next[26];
} ;

trie *root=new trie;
char word[300010];
char words[110];
int s;
int dp[300010];


void insert_trie()//向字典树中插入单词
{
	trie *p,*newnode;
	p=root;//容易忘,直接挂掉
	for(int i=0;words[i]!='\0';i++)
	{
		if(p->next[words[i]-'a']==NULL)
        {
            newnode=new trie;
            newnode->last=false;
            for(int j=0;j<26;j++)//注意新节点的初始化
                newnode->next[j]=NULL;
            p->next[words[i]-'a']=newnode;
            p=newnode;
        }
        else
        {
            p=p->next[words[i]-'a'];
        }
	}
	p->last=true;
}

void find_trie(int s)//查找串word
{
    trie *p=root;
    for(int i=s;word[i]!='\0';i++)
    {
        if(p->next[word[i]-'a']!=NULL)
        {
            if(p->next[word[i]-'a']->last==true)
            {
                dp[s]=(dp[s]+dp[i+1])%MOD;//居然忘记取模WA了。。。
            }
            p=p->next[word[i]-'a'];
        }
        else
        {
            return;
        }
    }
}

void getres()
{
    int len=strlen(word);
    dp[len]=1;//初始化
    for(int i=len-1;i>=0;i--)
    {
        find_trie(i);
    }
}

int main()
{
	int i,cas=0;
	while(~scanf("%s",word))
	{
	    memset(dp,0,sizeof(dp));
		for(i=0;i<26;i++)
			root->next[i]=NULL;
		scanf("%d",&s);
		for(i=0;i<s;i++)
		{
			scanf("%s",words);
			insert_trie();
		}
		getres();
		printf("Case %d: %d\n",++cas,dp[0]);
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值