/**
题目:UVALive 3942 Remember the Word
链接:https://vjudge.net/problem/UVALive-3942
题意:给定一个字符串(长度最多3e5)和m个单词(每个单词长度最多100)。单词都是不同的。该字符串可以由若干个单词组成,问最多有多少种组合方式。 思路:字典树+dp
用字典树处理好m个单词,定义dp[i]表示从i开始的字符串可以由单词组成的方式数。
那么dp[i] += dp[i+j]; j表示某个单词和字符串的[i,i+j-1]匹配。 */ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = ;
const int INF = 0x3f3f3f3f;
const int maxnode = 4e5+;///最多可能有多少个节点
const int maxn = 3e5+;
const int sigma_size = ;///26个字母
int dp[maxn];
char s[maxn], ss[maxn];安全1
int ch[maxnode][sigma_size];///由于很大,所以结构体内部放不下。要放在外面。
struct Trie{
int val[maxnode];
int sz;
int idx(char c){return c-'a';} void insert(char *s,int v)
{
int u = , n = strlen(s);
for(int 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;
} void query(int sta,int off)
{
int u = ;
int c = idx(s[sta]);
while(s[sta]!='\0'&&ch[u][c]){
if(val[ch[u][c]]){
dp[sta-off] = (dp[sta-off]+dp[sta+])%mod;
}
u = ch[u][c];
c = idx(s[++sta]);
off++;
}
}
}; int main()
{
int cas = ;
int n;
Trie trie;
while(scanf("%s",s)==)
{
scanf("%d",&n);
trie.sz = ;
memset(ch[], , sizeof ch[]);
for(int i = ; i < n; i++){
scanf("%s",ss);
trie.insert(ss,);
}
int m = strlen(s);
memset(dp, , sizeof dp);
dp[m] = ;
for(int i = m-; i>=; i--){
trie.query(i,);
}
printf("Case %d: %d\n",cas++,dp[]);
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

  7. UVALive 3942 Remember the Word(字典树+DP)

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

  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. Activex打包于发布完整版---微软证书制作

    众所周知,Activex组件没有进行有效的签名,在IE上无法安装的,除非你让用户手工开启“接收任何未签名的ActiveX”,这个很明显不现实.而组件签名需要证书,证书从哪里来,你可以选择付1000到3 ...

  2. 从0x00到0xFF的含义以及二进制到10进制的转换(转)

    转载自: http://www.cnblogs.com/brice/p/5343322.html 对于二进制来说,8位二进制我们称之为一个字节,二进制的表达范围值是从0b00000000-0b1111 ...

  3. Lina Group

    var query = from ll in proListRequestList group ll by new {ll.pro_id} into g select new ProListReque ...

  4. lua coroutine

    Lua中协程都放在表coroutine中. Lua协程的四个状态 挂起(suspended):一个协程被创建的时候,处于挂起状态,不会自动运行. 运行(running):coroutine.resum ...

  5. vue - vue-loader.conf

    'use strict' // 工具=> build/util.js const utils = require('./utils') // 配置=> build/config/index ...

  6. Android 开发之使用Eclipse Debug调试详解(转)

    转自 http://blog.csdn.net/xys289187120/article/details/6636331 1.在程序中添加一个断点 如果所示:在Eclipse中添加了一个程序断点 在E ...

  7. Android开发之应用程序更新实现

    近期给项目app做升级.对Android应用程序更新稍有研究,分享一下我的心得. 既然是更新,那么一定是要联网和下载的.所以联网和存储訪问权限时一定要有的: <!-- 权限申请 -->   ...

  8. JavaWeb 获取ip地址

      JavaWeb 获取ip地址 CreateTime--2018年5月31日17点56分 Author:Marydon import java.net.InetAddress; import jav ...

  9. Load和CPU利用率是如何算出来的 (转发)

    本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/tech/system/how_to_cal ...

  10. JBOSS整套开发组件整合和配置方法

    http://blog.csdn.net/laigood/article/details/5743712主要是集成jboss,jboss esb,jboss portal,jboss seam,jbo ...