(Trie) uvalive 3942 Remember the word
题意:告诉你一个母串和子串,能用多少种不同的方案组合出母串。
思路:字典树(显然)+DP
DP: dp[i]+=dp[j+1] i<=j<=m-1,且i到j的字符串能在字典树中找到。也就等价dp[i]=1*dp[j+1]
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
using namespace std; #define LL long long
const int maxn=*;
const int maxm=3e5+;
const int mod=;
struct Trie
{
int ch[maxn][];
int val[maxn];
int sz;
Trie()
{
sz=;
memset(ch[],,sizeof(ch[]));
}
int idx(char c)
{
return c-'a';
} void insert(char *s,int v)
{
int i,u=,n=strlen(s);
for(i=; i<n; i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
} e;
char str[maxm];
char a[];
LL dp[maxm];
int main()
{
int tt=;
while(scanf("%s",str)!=EOF)
{
int i,j,k,n,m,u;
scanf("%d",&n);
memset(e.ch[],,sizeof(e.ch[]));
e.sz=;
for(i=; i<n; i++)
{
scanf("%s",a);
e.insert(a,);
}
m=strlen(str);
memset(dp,,sizeof(dp));
dp[m]=;
for(i=m-; i>=; i--)
{
u=;
for(j=i; j<m; j++)
{
int c=e.idx(str[j]);
if(!e.ch[u][c])
break;
u=e.ch[u][c];
if(e.val[u])
dp[i]+=dp[j+];
}
dp[i]%=mod;
}
printf("Case %d: %lld\n",++tt,dp[]);
}
return ;
}
(Trie) uvalive 3942 Remember the word的更多相关文章
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 3942 Remember the Word
题意:给出一个由S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复 使用),有多少种方法? Sample Input abcd 4 a b cd ab Sample ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
随机推荐
- Hibernate 缓存机制二(转)
感谢:http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html 一.why(为什么要用Hibernate缓存?) Hibernate是一个 ...
- js获取节点
demo1: <!-- <div id="test" v="1">你好</div> --> // console.log(t ...
- 团体程序设计天梯赛-练习集L1-008. 求整数段和
L1-008. 求整数段和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆 给定两个整数A和B,输出从A到B的所有整数以及这些 ...
- linux ubuntu卸载软件
1.通过deb包安装的情况: 安装.deb包: 代码:sudo dpkg -i package_file.deb反安装.deb包: 代码:sudo dpkg -r package_name 2.通过a ...
- 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求
李洪强iOS开发本人集成环信的经验总结_06_发送好友请求 同步好友请求 异步好友请求
- Vue.js vui 饿了么Vue2.0的组件库
http://www.oschina.net/news/78038/vue-js-2-0-3 http://git.oschina.net/durcframework/vui http://eleme ...
- WPF之Behavior
本文主要是以实现拖动元素作为例子. 创建Behavior: 通常这个类会继承自Behavior<T>,其中T就是此Behavior服务的对象,在此处使用的是UIElement,也就是虽有的 ...
- mysql的group by应用
CREATE TABLE group_test ( id TINYINT(1) UNSIGNED NOT NULL, pubid TINYINT(1) UNSIGNED NOT NULL, user ...
- CodeForces114E——Double Happiness(素数二次筛选)
Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers ...
- P99、面试题13:在o(1)时间删除链表结点
题目:给定单向链表的头指针和一个结点指针,定义一个函数在o(1)时间删除该结点.链表结点与函数的定义如下:struct ListNode{ int m_nValue; List ...