题目链接: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. 查看/修改 Linux 时间和时区

    查看/修改Linux时区和时间 一.时区      1. 查看当前时区                 date -R      2. 修改设置时区          方法(1)            ...

  2. Android基础(13)——对话框 的使用

    转自:Android之 对话框 的使用 一个对话框通常是在当前Activity之前显示的一个小的窗口.下面的Activity失去了焦点,上面的对话框接收用户的交互信息.对话框通常用来作为提示以及直接与 ...

  3. rsync无密码实时增量同步

    rsync -azvP /rsync/ --password-file=/etc/rsyncd/rsyncd.password  ruiy@192.168.11.199:/rsync/ rsync - ...

  4. MySQL定时执行脚本(计划任务)命令实例

    在mysql中我们可以直接进行一些参数设置让它成定时为我们执行一些任务了,这个虽然可以使用windows或者linux中的计划任务实现,但是mysql本身也能完成 查看event是否开启 复制代码 代 ...

  5. unity3d InverseTransformPoint方法

    从歪果仁的脚本里看到了这个方法,查脚本,看脚本说明也没看懂,官方的说明是,变换位置从世界坐标到自身坐标,Transform.TransformPoint相反. 试验了一下得出这个结论,如果某一个物体A ...

  6. 6. js时间比较

    var v0 = ABS_DATESTRING(O_PARAMETER.FDate,"yyyy/MM/dd")var v_beginTime = v0 + " " ...

  7. 交换两个数-c++实现

    今天看了下交换数值的小程序,网上挺多的,整理了下,,因为参考较多,没一一给出链接,若原作者看到,可以留言,我会添加 // example_1_6_function_swap.cpp : 定义控制台应用 ...

  8. Flume NG简介及配置

    Flume下载地址:http://apache.fayea.com/flume/ 常用的分布式日志收集系统: Apache Flume. Facebook Scribe. Apache Chukwa ...

  9. SQL SERVER 批量插入记录

    --create function insertData(@count as int,@tsn as bigint,@id as int) --as --begin SET IDENTITY_INSE ...

  10. git撤销commit

    请参考该文章:http://www.cnblogs.com/ningkyolei/p/5026011.html 场景: 不小心commit了一个不应该commit的修改,但是还没有push,想撤销那个 ...