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

#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 遍历list

    #!/usr/bin/env python# -*- coding: utf-8 -*-if __name__ == '__main__':    list = ['html', 'js', 'css ...

  2. centos7中文显示为小方块~~啊啊啊 求大佬们解答

    这个问题困扰我很久了,刚好前几天注册了博客园,就想问问大佬们是怎么解决中文显示小方块的? 我试了很多办法,包括但不限于修改i18n配置文件,locale.conf,添加中文字体库等等等... 但都没有 ...

  3. JavaScript -- DOM事件

    什么是事件 事件就是文档或浏览器窗口中发生的一些特定的交互瞬间.比如你在网页上见到的鼠标点击一个按钮,按钮的颜色发生了变化,就是因为这个标签绑定了点击事件 鼠标事件 onload:页面加载时触发 on ...

  4. Cookie中存放数据l加密解密的算法

    public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...

  5. linux简单常用命令

    除了yum命令,还有些简单的命令,在此记录一下,加深记忆: free -h 查询内存和交换分区. rpm -qa | grep libaio 查看当前环境是否安装某rpm软件包

  6. 为什么要在函数内部声明 var that = this 呢

    看一个例子 $('#conten').click(function(){ //this是被点击的#conten var that =this; $('.conten').each(function() ...

  7. 【线段树分治 01背包】loj#6515. 「雅礼集训 2018 Day10」贪玩蓝月

    考试时候怎么就是没想到线段树分治呢? 题目描述 <贪玩蓝月>是目前最火爆的网页游戏.在游戏中每个角色都有若干装备,每件装备有一个特征值 $w$ 和一个战斗力 $v$ .在每种特定的情况下, ...

  8. python中字符串的一些用法

    一.字符串的拼接:      a=‘123’      b=‘abc’       d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ...

  9. 关于stm32优先级大小的理解

    转载自:https://www.cnblogs.com/ZKeJun/p/6112591.html 一. 组别:0>1>2>3>4   组别优先顺序(第0组优先级最强,第4组优 ...

  10. hdu 6318

    Long long ago, there was an integer sequence a.Tonyfang think this sequence is messy, so he will cou ...