题目链接: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. Jmeter组件3. HTTP Cookie Manager

    两个坑的地方 如果一个域(scope)内有两个cookie manager,Jmeter说,我分不清了,你自己看着办吧,所以不要没事找事,一个域内一个cookie manager够了 用户自定义coo ...

  2. Jmeter组件1. CSV Data Set Config

    位置:Test Plan | Add | Config Element | CSV Data Set Config 意义: 脚本参数化 节省CPU跟内存(可以准备好数据文件去代替动态生成数据,节约CP ...

  3. Android BaseAdapter的使用

    数据适配器有很多种,今天在这里记录一下最通用是适配器BaseAdapter. 首先说一下什么是适配器,这里我从网上找到一幅图片 由上图我们不难看出,所谓的适配器,就是数据与视图之间的桥梁.由它把数据绑 ...

  4. django1.7取消syncdb后不能创建model相应表的问题

    一.在运行一个django程序时,无法创建自定义model相应的表. 我检查seetings.py文件,发现自定义的app,blog已经写到INSTALLED_APPS中, INSTALLED_APP ...

  5. JS打开新窗口的2种方式

    1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a> 等效于js代码 ...

  6. 编译caffe报错:_ZN5boost16exception_detail10bad_alloc_D2Ev

    具体报错信息很长的. text._ZN5boost16exception_detail10bad_alloc_D2Ev[_ZN5boost16exception_detail10bad_alloc_D ...

  7. win8自动升级win8.1后 wampserver无法启动

    原因是升级时win8把其他的系统服务都给停止了. 解决办法是左键点击wamp的小图标,选择apache/mysql - service - 安装服务. 然后再选择启动服务,即可.

  8. JDBC basic

    http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm maven <dependency> <groupId>mysq ...

  9. Windows 下, SetTimer 定时器的研究.

    一直很困惑一个问题: 我设置了一个10秒的定时器,可是被调用的函数要花费30秒, 那待调用的函数第二次是什么时候调用的呢? 20秒, 40秒, 还是50秒呢.... 所以我进行了实验. 我写了一个类  ...

  10. oracle系统表查询

    oracle查询用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments -- 查询本 ...