注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还有,要注意strlen的时候,那个api的复杂度貌似是O(n)的,题目中提到输入数据的不同的test case之间有一个blank line,我理解成输出不同case之间要回车,OJ居然没判成PE,而是判成WA,这两天题写的真蛋疼(吐槽下)。

#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <iostream>
using namespace std; const int MAXN = ;
const int M = ; typedef long long int64; char ch[MAXN];
int64 dp[MAXN]; int id[ * ][], cnt;
bool flag[ * ]; class DicNode {
public:
bool flag;
DicNode *sons[];
DicNode() {
flag = false;
memset(sons, NULL, sizeof(sons));
}
}; class DicTree2 {
public:
DicTree2() {
cnt = ;
memset(id, -, sizeof(id));
memset(flag, false, sizeof(flag));
}
~DicTree2() { }
void insert(const char *s) {
int len = strlen(s);
int node = ;
for (int i = ; i < len; i++) {
if (id[node][s[i] - 'a'] == -) {
id[node][s[i] - 'a'] = cnt++;
}
node = id[node][s[i] - 'a'];
if (i == len - ) {
flag[node] = true;
}
}
}
bool query(const char *s) {
int len = strlen(s);
int node = ;
for (int i = ; i < len; i++) {
if (id[node][s[i] - 'a'] == -) {
return false;
}
node = id[node][s[i] - 'a'];
}
return flag[node];
}
int64 f(int b, int len) {
if (dp[b] != -) return dp[b];
dp[b] = ;
if (b == len) return dp[b] = ;
int node = ;
for (int i = b; i < len; i++) {
if (id[node][ch[i] - 'a'] != -) {
node = id[node][ch[i] - 'a'];
if (flag[node]) dp[b] = (dp[b] + f(i + , len)) % M;
} else {
break;
}
}
return dp[b];
}
}; class DicTree {
public:
DicNode *root;
DicTree() {
root = new DicNode();
}
~DicTree() {
if (NULL != root) {
free(root);
}
}
void free(DicNode *node) {
for (int i = ; i < ; i++) {
if (node->sons[i] != NULL) {
free(node->sons[i]);
}
}
delete node;
}
void insert(const char *s) {
int len = strlen(s);
DicNode *node = root;
for (int i = ; i < len; i++) {
if (node->sons[s[i] - 'a'] == NULL) {
node->sons[s[i] - 'a'] = new DicNode();
}
node = node->sons[s[i] - 'a'];
if (i == len - ) {
node->flag = true;
}
}
}
bool query(const char *s) {
int len = strlen(s);
DicNode *node = root;
for (int i = ; i < len; i++) {
if (node->sons[s[i] - 'a'] == NULL) {
return false;
}
node = node->sons[s[i] - 'a'];
}
return node->flag;
}
int64 f(int b, int len) {
if (dp[b] != -) return dp[b];
dp[b] = ;
if (b == len) return dp[b] = ;
DicNode *node = root;
for (int i = b; i < len; i++) {
if (node->sons[ch[i] - 'a'] != NULL) {
node = node->sons[ch[i] - 'a'];
if (node->flag) dp[b] = (dp[b] + f(i + , len)) % M;
} else {
break;
}
}
return dp[b];
}
}; int main() {
int c = ;
while (scanf("%s", ch) != EOF) {
int s;
scanf("%d", &s);
DicTree dic;
for (int i = ; i < s; i++) {
char str[];
scanf("%s", str);
dic.insert(str);
}
memset(dp, -, sizeof(dp));
printf("Case %d: %lld\n", ++c, dic.f(, strlen(ch)));
}
}

1401 - Remember the Word的更多相关文章

  1. UVA 1401 - Remember the Word(Trie+DP)

    UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...

  2. UVA 1401 Remember the Word

    字典树优化DP                                Remember the Word Time Limit: 3000MS   Memory Limit: Unknown ...

  3. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

  4. LA 3942 && UVa 1401 Remember the Word (Trie + DP)

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

  5. UVA - 1401 Remember the Word(trie+dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  6. Uva1401(字典树)

    1401 - Remember the Word Time limit: 3.000 seconds Neal is very curious about combinatorial problems ...

  7. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  8. UVa 1401 (Tire树) Remember the Word

    d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...

  9. 大白书 209 remember the word

    F - Remember the Word Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Sub ...

随机推荐

  1. .NET特性-Attribute

    两篇文章有助于学习Attribute特性的概念. http://blog.csdn.net/byondocean/article/details/6802111 http://www.cnblogs. ...

  2. 再也不要说,jquery动画呆板了

    1 show()方法和hide()方法 $("selector").show()  从display:none还原元素默认或已设置的display属性$("selecto ...

  3. python之类私有成员

    python类的成员前加双下划线"__", 则被看作"私有"成员. 实例不能简单地通过<instance>.<name>来访问. 但py ...

  4. EntityFramework中的datetime2异常的解决

    (转)   最近使用.net的Entity Framework构建网站数据层,给一个实体的DATETIME类型的属性赋值时 突然莫名奇妙显示有一个类型不匹配的异常如下: System.Data.Sql ...

  5. html onclick 传参数

    <a id="j-im" class="jd-im btn-gray gys-im" href="javascript:(0);" o ...

  6. matlab实现判断是否能否生成严格对角占优矩阵

    如题: function X = IsStrictDiagMatrix(A) % input: A matrix % output: The matrix after transformation % ...

  7. 【BZOJ 1026】 [SCOI2009]windy数

    Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? In ...

  8. redhat或centos关闭防火墙并开启sshd服务

    使用putty连接虚拟机的redhat连不上时处理方案: 这里使用的是VMware Workstation,  将宿主机与虚拟机之间的网络使用 ‘桥接方式’: 1.关闭宿主机与虚拟机的防火墙, 在re ...

  9. iOS10 配置须知-b

    在iOS10中,如果你的App想要访问用户的相机.相册.麦克风.通讯录等等权限,都需要进行相关的配置,不然会直接crash.需要在info.plist中添加App需要的一些设备权限. NSBlueto ...

  10. SVN 安装配置

    1,软件下载 到官方网站的下载二进制安装文件,来到二进制包下载部分,找到 Windows NT, 2000, XP and 2003部分,然后选择Apache 2.2 或者 Apache 2.4,这样 ...