UVA1401 Remember the word DP+Trie
问题描述
题解
DP,设\(opt_i\)代表前\(i\)个字符方案数。
Trie优化,刷表法。
\(\mathrm{Code}\)
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-'){
fh=-1;ch=getchar();
}
else fh=1;
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
x*=fh;
}
string s,t;
int n,opt[301000],m;
int ch[400003][26],tot,root=1;
int ed[400003];
const int mod=20071027;
int chk(char s){
return s-'a'+1;
}
void insert(){
int len=t.size(),p=root;
for(int i=0;i<len;i++){
int k=chk(t[i]);
if(!ch[p][k]) ch[p][k]=++tot;
p=ch[p][k];
}
++ed[p];
}
void query(int x){
int p=root,y=x+1,cnt=0;
while(p){
if(ed[p]){
opt[x+cnt]=(opt[x]+opt[x+cnt])%mod;
}
int k=chk(s[x+cnt]);
++cnt;p=ch[p][k];
}
}
int testcase;
void solve(){
memset(opt,0,sizeof(opt));opt[0]=1;
memset(ch,0,sizeof(ch));tot=1;memset(ed,0,sizeof(ed));
cin>>m;
for(int i=1;i<=m;i++){
cin>>t;
insert();
}
for(int i=0;i<s.size();i++){
query(i);
}
cout<<"Case "<<testcase<<": "<<opt[s.size()]<<endl;
}
int main(){
ios::sync_with_stdio(false);
while(1){
s="";cin>>s;++testcase;
if(!s.size()) break;
solve();
}
return 0;
}
UVA1401 Remember the word DP+Trie的更多相关文章
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- UVA1401 Remember the Word 字典树维护dp
题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- uva1401 dp+Trie
这题说的是给了一个长的字符串长度最大300000,又给了4000个单词 单词的长度不超过100.计算这个字符串能组成多少种不同单词的组合,求出方案总数.dp[i]以第i个字符为开始的字符串能有多少种的 ...
- Remember the Word UVALive - 3942(dp+trie)
题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- Codeforces 633C Spy Syndrome 2(DP + Trie树)
题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...
随机推荐
- Angular(06)- 为什么数据变化,绑定的视图就会自动更新了?
这里提一点,前端三大框架(Angular,React,Vue)的数据驱动来更新视图的原理,即 MVVM 的实现. 为什么数据发生变化,绑定的视图就会刷新了呢? 以下是我的个人理解,仅供参考: 在还是 ...
- 第03讲 fragment
Fragment 官网文档:https://developer.android.google.cn/guide/components/fragments 什么是Fragment 在手机上,Activi ...
- 从0系统学Android--3.6 RecyclerView
从0系统学Android--更强大的滚动控件---RecyclerView 本系列文章目录:更多精品文章分类 本系列持续更新中.... 参考<第一行代码> 首先说明一点昨天发了一篇关于 L ...
- 如何实现用户的历史记录功能(最多n条)
使用容量为n的队列存储历史记录 使用标准库collections中的deque,它是一个双端循环队列 from collections import deque q = deque([], 5) #参 ...
- MySQL 8.0部分弃用的参数整理
最近整理了一下MySQL 8.0的自动化安装,其中用到了一个MySQL 5.7版本的自定义配置文件,由于没有对(MySQL 8.0)做针对性修改,导致安装过程中出现了一些错误其中部分原因就是MySQL ...
- MySQL相关参数总结
保留个原文链接,避免被爬虫爬了过去,以便后续更正补充:https://www.cnblogs.com/wy123/p/11273023.html MySQL参数繁多,是一个需要根据具体业务.软硬件环境 ...
- linux安装数据时报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo.
在redhat6.5上安装Oracle时,最后使用oracle用户执行runInstaller 报错如下,无法连接到安装有xmanager的windows服务器,也就无法图形化安装oracle ora ...
- 18c & 19c Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 2485237.1)
18c & 19c Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 2485237.1) APPLIES T ...
- 安装oracle11g客户端
1.将压缩包instantclient_11_2 解压到数据库安装目录下(D:\app\hisoft\product\11.2.0),即与dbhome_1同级目录 2.添加环境变量 至此,oracle ...
- web攻击与防御技术-平台搭建与暴力破解
平台搭建是首先安装xampp并把pikachu的压缩文件解压在HTdocs下 然后 点击后显示 安装成功 首先随便输入一些东西 然后用burpsuite抓包 对username和password字段进 ...