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

#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. JS实用技术

    JS外部引用其他文件(建议) <script src="myScript1.js"></script> JS输出显示方式 使用 window.alert() ...

  2. java基础—java制作证书的工具keytool

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  3. AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?

    问题 An error occurred while starting the application. ArgumentException: AddDbContext was called with ...

  4. iOS 导航栏遮挡问题 --- iOS开发系列 ---项目中成长的知识七

    不知大家有没有遇见过自己写的tableview被导航栏遮挡住的问题,反正我是遇见过! 因为在ios7以后所有的UIViewController创建后默认就是full Screen的,因此如果带导航栏的 ...

  5. VS连接SQL Server 2008,并实现登录和注册功能

    --------------------- 作者:Cambridge 来源:CSDN 原文:https://blog.csdn.net/cambridgeacm/article/details/797 ...

  6. ajax $.post 一直报 Forbidden (CSRF token missing or incorrect.)

    由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_pag ...

  7. vue渲染函数&JSX

    Vue推荐在绝大多数情况下使用template来创建你的HTML.然而在一些场景中,你真的需要JavaScript的完全编程能力,这时你可以使用render函数,它比template跟接近编译器. 虚 ...

  8. 【linux】服务说明

     引用自<鸟哥的linux私房菜>  http://cn.linux.vbird.org/linux_server/0210network-secure_3.php 服务名称 服务内容 a ...

  9. Spring Cloud构建微服务架构(二)分布式配置中心

     注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...

  10. 使用 Python 编写登陆接口

    # 使用 Python 编写登陆接口# Create Date: 2017.10.31 Tuesday# Author: Eric Zhao# -*- coding:utf-8 -*-'''编写登陆接 ...