题意:

有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法

分析:

dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len(x)]);单词x是i开始的字符串的前缀。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define maxnode 500000
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int dp[maxnode];
struct Trie{
int ch[maxnode][];
int val[maxnode];
int num;
void init(){num=;memset(ch[],,sizeof(ch[]));}
int idx(char c){
return c-'a';
}
void build(char *s,int v){
int u=,n=strlen(s);
for(int i=;i<n;++i){
int c=idx(s[i]);
if(!ch[u][c]){
memset(ch[num],,sizeof(ch[num]));
val[num]=;
ch[u][c]=num++;
}
u=ch[u][c];
}
val[u]=v;
}
int query(char *s,int st){
int u=,sum=,n=strlen(s);
for(int i=st;i<n;++i)
{
int c=idx(s[i]);
u=ch[u][c];
if(u==)return sum;
if(val[u]){
sum+=dp[i+];
sum%=mod;
}
}
return sum;
}
}trie;
int main()
{
char str[],str1[];
int m;
while(~scanf("%s",str)){
scanf("%d",&m);
trie.init();
for(int i=;i<m;++i){
scanf("%s",str1);
trie.build(str1,);
}
memset(dp,,sizeof(dp));
int len=strlen(str);
dp[len]=;
for(int i=;i<=len;++i)
dp[len-i]=trie.query(str,len-i);
printf("%d\n",dp[]);
}
return ;
}

LA3942-Remember the Word(Trie)的更多相关文章

  1. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  2. C#操作Office.word(二)

    在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...

  3. 最全总结 | 聊聊 Python 办公自动化之 Word(中)

    1. 前言 上一篇文章,对 Word 写入数据的一些常见操作进行了总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 相比写入数据,读取数据同样很实用! 本篇文章,将谈谈如何全面读取 ...

  4. 最全总结 | 聊聊 Python 办公自动化之 Word(下)

    1. 前言 关于 Word 文档的读写,前面两篇文章分别进行了一次全面的总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 最全总结 | 聊聊 Python 办公自动化之 Word( ...

  5. WinForm小白的WPF初试一:从PropertyGrid控件,输出内容到Word(上)

    学WinForm也就半年,然后转到WPF,还在熟悉中.最近拿到一个任务:从PropertyGrid控件,输出内容到Word.难点有: 一.PropertyGrid控件是WinForm控件,在WPF中并 ...

  6. 数据结构作业——word(栈)

    Description TonyY 是一个 word 小白,今天他对 word 中撤销和恢复功能特别感兴趣,玩耍了一个上午(mdzz~) ,现在他知道了它们的功能和快捷键:撤销:ctrl+z,可以撤销 ...

  7. C#操作Word (1)Word对象模型

    Word对象模型  (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object ...

  8. C#操作Office.word(一)

    该文章主要是讲述如何使用VS2010创建word文档,因为在项目中我们可能需要点击一个按钮把数据库中的项目表单或图片显示到word文档中,因此该文章主要分析如何使用VS2010创建word文档并填写相 ...

  9. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. Ubuntu 设置root用户登录

    由于 Ubuntu 是基于 Debian 的 linux 操作系统,在默认的情况下,是没有超级用户(superuser, root)的,但有些系统操作必须有超级用户的权限才能进行,如手动释放内存等.  ...

  2. 在一个工程管理多个应用-b

    Demo:http://download.csdn.net/detail/u012881779/9166527 本文的产生是因产品经理提出的特殊需求: 一个针对多所学校的应用,对不同学校需要分别使用一 ...

  3. 1038: [ZJOI2008]瞭望塔 - BZOJ

    Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...

  4. Exploring the 7 Different Types of Data Stories

    Exploring the 7 Different Types of Data Stories What makes a story truly data-driven? For one, the n ...

  5. hdu 4648

    求一个数列中  去掉一些连续的数之后 不改变对m取余后的值 就是求一个最长的连续子序列 对m取余后为0 我的方法可能比较水 #include<iostream> #include<c ...

  6. [BEC][hujiang] Lesson03 Unit1:Working life ---Grammar & Listening & Vocabulary

    3 Working life p8 Grammar Gerund and infinitive(动名词和不定式) 一般而言:        1 动词后面接动名词还是不定式没有特定规则,主要取决于语言习 ...

  7. CF 136B Ternary Logic

    http://codeforces.com/problemset/problem/136/B 题意 :就是说tor是一个三进制的运算,代表的是两个三进制数的运算,两个三进制数按位逐一相加后对三取余,没 ...

  8. DJANGO的ORM的Q查询作多字段外键的模糊查询样码

    工作中用到的,存照一下. from django.db.models import Q if self.kwargs.has_key('search_pk'): search_pk = self.kw ...

  9. MyBatis的分页操作(MySQL)

    1.无条件分页: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC ...

  10. Trainning Guide的代码

    今天无意间找到了训练指南的网上代码,都是刘汝佳写的,在这. 今天在做这题1400 - "Ray, Pass me the dishes!",我写的线段树的思路跟上次的Frequen ...