题目大意:一个字符串,可以分解成若干英语单词的连接(单词可以重复使用),求有多少种方法?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; const int maxl=;
const int maxwn=;
const int maxwl=;
const int MOD=;
const int maxnode=;
const int sigma_size=; struct Trie
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void clear(){sz=;memset(ch[],,sizeof(ch[]));}
int idx(char c){ return c-'a'; }
void insert(const char *s,int v)
{
int i,c,u=,n=strlen(s);
for(i=;i<n;i++)
{
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 find_prefixes(const char *s,int len,vector<int> &ans)
{
int i,c,u=;
for(i=;i<len;i++)
{
if(s[i]=='\0') break;
c=idx(s[i]);
if(!ch[u][c]) break;
u=ch[u][c];
if(val[u]) ans.push_back(val[u]);
}
}
}trie; int d[maxl],len[maxwn],S;
char text[maxl],word[maxwl];
int main()
{
int icase=,i,j,L;
while(scanf("%s %d",text,&S) == )
{
trie.clear();
for(i=;i<=S;i++)
{
scanf("%s",word);
len[i]=strlen(word);
trie.insert(word,i);
}
memset(d,,sizeof(d));
L=strlen(text);
d[L]=;
for(i=L-;i>=;i--)
{
vector<int> p;
trie.find_prefixes(text+i,L-i,p);
for(j=;j<p.size();j++)
d[i]=(d[i]+d[i+len[p[j]]])%MOD;
}
printf("Case %d: %d\n",icase++,d[]);
}
return ;
}

La 3942 字符串+dp的更多相关文章

  1. [LA 3942] Remember the Word

    Link: LA 3942 传送门 Solution: 感觉自己字符串不太行啊,要加练一些蓝书上的水题了…… $Trie$+$dp$ 转移方程:$dp[i]=sum\{ dp[i+len(x)+1]\ ...

  2. 【BZOJ 2121】 (字符串DP,区间DP)

    2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...

  3. AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)

    引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度  pos[i][j]表示从i开始的j字符最早出现位 ...

  4. NOIP2015Day2T2子串(字符串dp)

    又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...

  5. Codeforces 1150D(字符串dp)

    反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...

  6. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

  7. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

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

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

  9. Trie + DP LA 3942 Remember the Word

    题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...

随机推荐

  1. python之道13

    看代码分析结果 func_list = [] for i in range(10): func_list.append(lambda :i) v1 = func_list[0]() v2 = func ...

  2. k8s基于canel的网络策略

    Kubernetes能够把集群中不同Node节点上的Pod连接起来,并且默认情况下,每个Pod之间是可以相互访问的.但在某些场景中,不同的Pod不应该互通,这个时候就需要进行访问控制.亲测:在kube ...

  3. How To Add Swap Space on Ubuntu 16.04

    Introduction One of the easiest way of increasing the responsiveness of your server and guarding aga ...

  4. 如何用纯 CSS 创作一个过山车 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/KBxYZg/ 可交互视频 此视频是 ...

  5. 【mysql】mysql has gone away

    原文 http://www.jb51.net/article/23781.htm MySQL server has gone away 问题的解决方法 投稿:mdxy-dxy 字体:[增加 减小] 类 ...

  6. Python爬虫二

    常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...

  7. LeetCode(278)First Bad Version

    题目 You are a product manager and currently leading a team to develop a new product. Unfortunately, t ...

  8. LeetCode(134) Gas Station

    题目 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  9. Post页面爬取失败__编码问题

    python3爬取Post页面时, 报以下错误 "POST data should be bytes or an iterable of bytes. It cannot be of typ ...

  10. 使用Blend的一些问题和技巧

    WPF开发,界面处理首选Blend,如果你开发了两年WPF都没接触过blend(当然这种几率不高),或者你刚接触WPF,可以考虑使用Blend,这货也算得上一个神器,上手也不难.以下有两位讲得不错,大 ...