La 3942 字符串+dp
题目大意:一个字符串,可以分解成若干英语单词的连接(单词可以重复使用),求有多少种方法?
#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的更多相关文章
- [LA 3942] Remember the Word
Link: LA 3942 传送门 Solution: 感觉自己字符串不太行啊,要加练一些蓝书上的水题了…… $Trie$+$dp$ 转移方程:$dp[i]=sum\{ dp[i+len(x)+1]\ ...
- 【BZOJ 2121】 (字符串DP,区间DP)
2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...
- AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)
引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度 pos[i][j]表示从i开始的j字符最早出现位 ...
- NOIP2015Day2T2子串(字符串dp)
又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...
- Codeforces 1150D(字符串dp)
反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Trie + DP LA 3942 Remember the Word
题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...
随机推荐
- spring mvc + freemarker 整合
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.s ...
- EXC_BAD_ACCESS调试
有时候,你会遇到由EXCBADACCESS造成崩溃. 这篇文件会告诉你什么是EXCBADACCESS,以及它产生的原因.我还会提供一些EXCBADACCESS错误的解决方案. 1. 什么是 EXCBA ...
- abaqus中的约束
1.tie -绑定约束:作用是将模型的两部分区域绑定在一起,二者之间不发生相对运动,相当于焊在一起. 2.rigid body--刚体约束--使一个模型区域刚体化,这个区域可以是一系列节点,单元等,刚 ...
- html5新结构标签
html5新结构标签 <header> 定义 section 或 page 的页眉,也就是定义头部的标签. <footer> 定义 section 或 page 的页脚. & ...
- oop中 限制文件类型和大小
<?php /** * Created by IntelliJ IDEA. * User: jiabinwang * Date: 7/5/18 * Time: 8:46 PM */ namesp ...
- 通过IAR工程文件查看对应IAR版本号
IAR使用技巧——如何使用合适的版本打开IAR工程 2014年07月05日 23:49:08 xukai871105 阅读数:12895 标签: IAR 更多 个人分类: 嵌入式ARM 0.前言 ...
- Down the Pyramid
Do you like number pyramids? Given a number sequence that represents the base, you are usually suppo ...
- ssh登陆之忽略known_hosts文件
在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- HDU 5489 Removed Interval DP 树状数组
题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...
- oracle游标遍历
--创建存储过程 CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p (--参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型. is_ ...