LA3942-Remember the Word(Trie)
题意:
有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)的更多相关文章
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- C#操作Office.word(二)
在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...
- 最全总结 | 聊聊 Python 办公自动化之 Word(中)
1. 前言 上一篇文章,对 Word 写入数据的一些常见操作进行了总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 相比写入数据,读取数据同样很实用! 本篇文章,将谈谈如何全面读取 ...
- 最全总结 | 聊聊 Python 办公自动化之 Word(下)
1. 前言 关于 Word 文档的读写,前面两篇文章分别进行了一次全面的总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 最全总结 | 聊聊 Python 办公自动化之 Word( ...
- WinForm小白的WPF初试一:从PropertyGrid控件,输出内容到Word(上)
学WinForm也就半年,然后转到WPF,还在熟悉中.最近拿到一个任务:从PropertyGrid控件,输出内容到Word.难点有: 一.PropertyGrid控件是WinForm控件,在WPF中并 ...
- 数据结构作业——word(栈)
Description TonyY 是一个 word 小白,今天他对 word 中撤销和恢复功能特别感兴趣,玩耍了一个上午(mdzz~) ,现在他知道了它们的功能和快捷键:撤销:ctrl+z,可以撤销 ...
- C#操作Word (1)Word对象模型
Word对象模型 (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object ...
- C#操作Office.word(一)
该文章主要是讲述如何使用VS2010创建word文档,因为在项目中我们可能需要点击一个按钮把数据库中的项目表单或图片显示到word文档中,因此该文章主要分析如何使用VS2010创建word文档并填写相 ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- mybatis foreach标签
一.批量插入数据 示例:添加订单商品表 1.模型层的相应代码 /** * 添加订单商品表 * @param ordergoods * @return */ public boolean addOrde ...
- javascript中跨源资源共享
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十) 通过XHR实现Ajax通信的一个主要限制,来源于跨域安全策略.默认情况下,XHR对 ...
- [转载]获取当前日期和农历的js代码
原文地址: http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期时间: function GetCurrentDa ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- spoj 39
DP 完全背包问题 的 d[i] = min(d[i], d[i-w]+p) d[i]表示当总重量为i时可装的最小价值 #include <cstdio> #include &l ...
- http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/
http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/
- jquery中事件重复绑定以及解绑问题
一般的情况下,对于这种情况,我们常规的思路是,先解绑,再绑定,如下: $(selector).unbind('click').bind('click',function(){....}); 当这样会有 ...
- objective-c 在线视频 学习资料...
---视频 http://www.lanou3g.com/newslist.php?cid=7 http://edu.51cto.com/lesson/id-15489.html http://www ...
- 安装和使用screen
安装和使用screen 安装screenyum可以在线安装screenyum install screen 使用screen1.创建screen会话;进入Xshell,运行以下:screen 2.离开 ...
- Java之iterator迭代器和iterable接口
java.lang.Iterable java.util.Iterator Iterator是迭代器类,而Iterable是接口. 好多类都实现了Iterable接口,这样对象就可以调用iterato ...