#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int M = ;
const int maxn = + ;
const int maxnode = +;
const int _size = ; struct Trie {
int ch[maxnode][_size];
int val[maxnode];
int sz;
Trie() {
sz = ;
memset(ch[], , sizeof (ch[]));
}
int idx(int c) {return c - 'a';} void insert(char *s, int v) { //插入字符串s和权值v
int u = , n = strlen(s);
for(int i = ; i < n; i++) {
int 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) { //寻找字符串s的前缀
int u = ;
for(int i = ; i < len; i++) {
if(s[i] == '\0') break;
int c = idx(s[i]);
if(!ch[u][c]) break;
u = ch[u][c];
if(val[u] != ) ans.push_back(val[u]);
}
} void clear() {
sz = ;
memset(val, , sizeof val);
memset(ch[], , sizeof (ch[]));
}
}; Trie T;
char S[maxn], s[];
int d[maxn]; int cal_d() {
int L = strlen(S);
d[L] = ;
for(int i = L-; i >= ; i--){
vector<int> p;
T.find_prefixes(S+i, L-i, p);
for(int j = ; j < p.size(); j++) {
d[i] = (d[i] + d[i + p[j]]) % M;
}
}
return d[];
}
int main(int argc, const char * argv[]) {
int tt = ;
while(scanf("%s", S) == ){
T.clear();
memset(d, , sizeof d);
int m;
scanf("%d", &m);
for(int i = ; i < m; i++) {
scanf("%s", s);
T.insert(s, strlen(s));
}
int ans = cal_d();
printf("Case %d: %d\n", ++tt, ans);
}
return ;
}

解题思路:

  令d(i)表示从字符i开始的字符串(即S[i...L])的分解方案数。

  则状态转移方程:

  d(i) = sum{d(i+len(x)) | x为S[i..L]的前缀}

LA 3942 ——Trie (前缀树)、DP的更多相关文章

  1. la 3942 Rember_前缀树

    #include <iostream> #include<cstdio> #include<cstring> using namespace std; #defin ...

  2. [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  3. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

  4. leetcode 208. 实现 Trie (前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  5. 第15个算法-实现 Trie (前缀树)(LeetCode)

    解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...

  6. 实现 Trie (前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  7. 力扣208——实现 Trie (前缀树)

    这道题主要是构造前缀树节点的数据结构,帮助解答问题. 原题 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = ...

  8. Java实现 LeetCode 208 实现 Trie (前缀树)

    208. 实现 Trie (前缀树) 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie() ...

  9. 数据结构—— Trie (前缀树)

    实现一个 Trie (前缀树),包含 插入, 查询, 和 查询前缀这三个操作. Trie trie = new Trie(); trie.insert("apple"); trie ...

  10. 力扣 - 208. 实现Trie(前缀树)

    目录 题目 思路 代码 复杂度分析 题目 208. 实现 Trie (前缀树) 思路 在我们生活中很多地方都用到了前缀树:自动补全,模糊匹配,九宫格打字预测等等... 虽然说用哈希表也可以实现:是否出 ...

随机推荐

  1. linux通常使用的 rc 和 .(点)文件

    文件名 描述 ~/.bash_login 请参考“man bash”.如果 ~/.bash_profile 不存在,bash 则将 ~/.bash_login 作为 ~/.bash_profile 处 ...

  2. 【水滴石穿】react-native-ble-demo

    项目的话,是想打开蓝牙,然后连接设备 点击已经连接的设备,我们会看到一些设备 不过我这边在开启蓝牙的时候报错了 先放作者的项目地址: https://github.com/hezhii/react-n ...

  3. Tomcat的原理

    Tomcat是一种WEB容器:它主要实现了javaEE中Servlet.JSP规范,同时提供HTTP服务. 它主要由一个Serve服务器,Server服务器中存在多个Service服务,Service ...

  4. IDEA:将WEB-INF\lib下的Jar包添加到项目中

    打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S] 左侧选中Modules,在Dependecies中,点击右侧"+"号,选择JARS or ...

  5. 2015全球商业地产商影响力排行TOP10:中国占据5个

    2015全球商业地产商影响力排行TOP10:中国占据5个 1.西蒙丨依然最有影响力 发展概况:西蒙公司是北美商业地产(专题阅读)的王者,美最大商业地产REITS,1960年最初只以开发为主,1993年 ...

  6. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第九章:贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第九章:贴图 代码工程地址: https://github.com/j ...

  7. iOS 适配iPhoneX上tableHeaderView发生了高度拉伸、UI出现的空白间距

    记录下前阵子遇到的一个问题,草稿箱里记录的有点潦草,讲下大概吧. 异常如下,粉色区域作为tableHeader放上去的(注意不是sectionHeader) header初始化之后一切正常,frame ...

  8. Maven中央存储库 和 如何从Maven远程存储库下载?

    当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载.首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源,如果没有找到,然后把它会从默 ...

  9. Spring Security入门篇——标签sec:authorize的使用

    Security框架可以精确控制页面的一个按钮.链接,它在页面上权限的控制实际上是通过它提供的标签来做到的 Security共有三类标签authorize authentication accessc ...

  10. 2015 Objective-C 三大新特性

    http://www.cocoachina.com/ios/20150617/12148.html Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的 ...