题目链接: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. 潜入ICU的四逆汤

    制附子50克(先煎2小时).干姜15克.炙甘草10克.桂枝10克.上好肉桂10克(后下).煎取药液300毫升,分三次鼻饲.这是昨天我给A先生病危的父亲开的处方:四逆汤加肉桂.桂枝. 昨天上午的门诊到2 ...

  2. memcache c++使用

    memcache作为一个内存数据库,快速查询的功能. 在c++下面使用memcache需要一个memcache client.c++版本的是libmemcached http://libmemcach ...

  3. Linux下grep命令

    2.grep命令 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本 ...

  4. strlen和sizeof

    1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型.该类型保证能容纳实现所建立的最大对象的字节大小. 2.sizeof是算符,strlen是函数. ...

  5. C++Builder Berlin 编译

    编译工程,一个单元有错误,就停下了, 能不能把所有单元都编译,一次处理所有单元的错误?

  6. XidianOJ 1182 Chinese Paladin – Qi’s troubles

    题目描述 As we all know, Xiahou Jinxuan (Chinese Paladin 5 Prequel's protagonist) and Yue Jinzhao (Chine ...

  7. APP分发渠道的竞争分析

    一. 最近几年,手机APP市场发展非常迅速,随着手机的硬件水平的不断升级,大量资本涌入,越来越多的开发者从桌面平台开发转移到移动平台开发,面对数以万计的手机APP,如何推广自己的APP成了难题,APP ...

  8. {Links}{Matting}{Saliency Detection}{Superpixel}Source links

    自然图像抠图/视频抠像技术发展情况梳理(image matting, alpha matting, video matting)--计算机视觉专题1 http://blog.csdn.net/ansh ...

  9. 64 位 Ubuntu 下 android adb 不可用解决方法

    解决方案: 安装ia32-libs 在终端执行 sudo apt-get install ia32-libs 其间会提示所依赖的某些包不存在,直接 sudo apt-get 安装即可.

  10. Mysql错误信息汇总

    目录: 1. Every derived table must have its own alias 内容: 1. Every derived table must have its own alia ...