题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1943

题意:一个长字符串和多个短字符串,求短字符串有多少种方式组成长字符串。

状态转移方程: dp[i] = sum(d[i + len(x)])  (x是s[i...L]的前缀)

对于每个i,如果直接暴力寻找s[i...L]的前缀,复杂度为O(nm) (n为短字符串的个数,m为短字符串的长度),肯定会超时。

既然是求前缀,那么可以使用前缀树(字典树)来解决此问题。用字典树寻找前缀的复杂度为O(m)。

#include <cstdio>
#include <iostream>
#include <cstring>
#define maxn 400100
#define maxm 300010
#define mod 20071027
#define sigma_size 26
using namespace std; char str[maxm], tstr[];
int dp[maxm], ch[maxn][sigma_size], val[maxn], sz; struct Trie
{
int get_id(char c)
{
return c - 'a';
} void insert(char* str, int v)
{
int u = , len = (int)strlen(str);
for (int i = ; i < len; ++i)
{
int id = get_id(str[i]);
if (!ch[u][id])
{
memset( ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][id] = sz++;
}
u = ch[u][id];
}
val[u] = v;
} int query(char* str, int start)
{
int u = , result = , len = (int)strlen(str), next;
for (int i = ; i < len; ++i)
{
int id = get_id(str[i]);
next = ch[u][id];
if (next)
{
if (val[next])
result = (result + dp[i + start + ])%mod;
}
else
break;
u = next;
}
return result;
}
}; void init(); int main(void)
{
int ca = , n;
while (scanf("%s", str) != EOF)
{
init();
int len = (int)strlen(str); scanf("%d", &n);
Trie trie = Trie();
while (n--)
{
scanf("%s", tstr);
trie.insert( tstr, );
} dp[len] = ;
for (int i = len - ; i >= ; i--)
{
dp[i] = trie.query( str + i, i);
}
printf("Case %d: %d\n", ca++, dp[]);
}
return ;
} void init()
{
sz = ;
memset( dp, , sizeof(dp));
memset( ch[], , sizeof(ch[]));
memset( val, , sizeof(val));
}

UVALive 3942 Remember the Word(字典树+DP)的更多相关文章

  1. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  2. LA 3942 - Remember the Word 字典树+DP

    看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  3. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  7. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...

  8. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. UVALive 3942 字典树+dp

    其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...

随机推荐

  1. 神奇的C语言

    当然下面列出来的几点都是C的基础用法,只不过是这些用法可能平时不会被注意.所以很多东西第一次看到的时候,可能会觉得很怪异,但是细细想想就能很好的理解,也就能更好的清楚C语言的一些特性.但是在具体的编码 ...

  2. MFCButton Memory leak(内存泄露问题)

    http://m.blog.csdn.net/blog/haoekin/8851219 1.无法显示右边箭头的问题 无论怎么折腾都没显示不出来,微软给的示例又能显示,度娘和谷歌也都不知道,经过不断地探 ...

  3. JS 之作用域链和闭包

    1.JS无块级作用域 <script> function Main(){ if (1==1){ var name = "alex"; } console.log(nam ...

  4. TIJ读书笔记05-this关键字

    TIJ读书笔记05-this关键字 概述 this的用法 static方法 概述 两个对象同时调用一个方法,那么jvm怎么知道是哪个方法调用的呢. 我们在书写的时候会写成a.fun(1) 或者b.fu ...

  5. (转)jQuery中的extend()方法

    本文转自:http://www.xiabingbao.com/jquery/2015/05/30/jquery-extend 原文的排版要比这里美观很多,建议去原文查看.本文仅仅作为个人的mark,方 ...

  6. sqoop job 踩过的坑

    sqoop 执行可以以job形式 也可以执行用命令执行,再用sqoopjob时,踩了几个坑,分享一下 1.服务器重启 由于服务器增加硬盘,需要重启后,发现sqoop job 无法执行,报连接数据库IO ...

  7. Linux_06------Linux的磁盘管理

    进制编码 * 3.格式化交换分区 * 4.启用交换分区 * * 1.2. * fdisk /dev/sdb * p * t * 6(分区编号) * L(查看编码列表),找到SWAP交换分区标号 * 8 ...

  8. DDK Build的DIRS和SOURCE文件

    DDK Build编译的时候,使用3个文件来描述被编译的源码,其中SOURCES和Makefile是必须的,而DIRS则只在划分目录的时候有用.Makefile在这里作用并不大但是必须和SOURCES ...

  9. C#模拟浏览器发get,post请求

    调用: JArray ja = (JArray)JsonConvert.DeserializeObject("["+GetDataPost()+"]"); Re ...

  10. linux关闭防火墙

    查看防火墙状态: sudo service iptables status linux关闭防火墙命令: sudo service iptables stop linux启动防火墙命令: sudo se ...