la 3942 Rember_前缀树
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 400010
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int ch[N][30];
int val[N];
int cnt;
char str[N];
int d[N];
int idx(int c){
return c-'a';
}
void insert(char *s){
int i,n=strlen(s);
int c,u=0;
for(i=0;i<n;i++){
c=idx(s[i]);
if(!ch[u][c]){
memset(ch[cnt],0,sizeof(ch[cnt]));
val[cnt]=0;
ch[u][c]=cnt++;
}
u=ch[u][c];
}
val[u]=n;
}
void search(char *s,int i,int len){
int j;
int c,u=0;
for(j=0;j<len;j++){
c=idx(s[j]);
if(!ch[u][c])
break;
u=ch[u][c];
if(val[u])
d[i]=(d[i]+d[i+val[u]])%20071027;
}
}
int main(int argc, char** argv) {
int num=1;
int n,i,k;
char s[N];
while(scanf("%s",str)!=EOF){
cnt=1;
val[0]=0;
memset(ch[0],0,sizeof(ch[0]));
n=strlen(str);
scanf("%d",&k);
while(k--){
scanf("%s",s);
insert(s);
}
memset(d,0,sizeof(d));
d[n]=1;
for(i=n-1;i>=0;i--)
search(str+i,i,n-i);
printf("Case %d: %d\n",num++,d[0]);
}
return 0;
}
la 3942 Rember_前缀树的更多相关文章
- [LA 3942] Remember the Word
Link: LA 3942 传送门 Solution: 感觉自己字符串不太行啊,要加练一些蓝书上的水题了…… $Trie$+$dp$ 转移方程:$dp[i]=sum\{ dp[i+len(x)+1]\ ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- HDU1671——前缀树的一点感触
题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树
另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...
- trie树(前缀树)
问题描述: Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- 【暑假】[实用数据结构]前缀树 Trie
前缀树Trie Trie可理解为一个能够快速插入与查询的集合,无论是插入还是查询所需时间都为O(m) 模板如下: +; ; struct Trie{ int ch[maxnode][sigma_siz ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 1042.D Petya and Array 前缀 + 树状数组
11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...
随机推荐
- Linux APP源码级编译安装
首先需要了解下tar包. 以下文章作出解释了: http://www.cnblogs.com/laipDIDI/articles/2214270.html http://baike.baidu.com ...
- android fragment 跳到另一个fragment
一共有4个fragment,分别是contact(联系人),friends(朋友),search(查找),more(更多).使用的都是同一个布局,每个fragment中都有四个内部按钮,可以切换到其他 ...
- 自制简单表单验证relative与absolute定位
html结构,用到了label与span <label class="relative"><input type="text" name=&q ...
- cf475A Bayan Bus
A. Bayan Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 理解最短路径——迪杰斯特拉(dijkstra)算法
原址地址:http://ibupu.link/?id=29 1. 迪杰斯特拉算法简介 迪杰斯特拉(dijkstra)算法是典型的用来解决最短路径的算法,也是很多教程中的范例,由荷兰计算机科 ...
- Binary Tree Level Order Traversal 解答
Question Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...
- python刷取CSDN博文访问量之一
python刷取CSDN博文访问量之一 作者:vpoet 注:这个系列我只贴代码,代码不注释.有兴趣的自己读读就懂了,纯属娱乐,望管理员抬手 若有转载一定不要注明来源 #coding=utf-8 ...
- C pointers
指向整型数组指针int (*p)[10] = matrix;增加这个指针的值使它指向下一个整型数组 指向整型指针int *pi = &matrix[0][0];int *pi = &m ...
- Virtual Friends(并查集+map)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 【HeadFirst 设计模式总结】1.策略模式
1.书中举了一个鸭子类的设计,有些会飞或者会叫,有些不会飞可能也不会叫,用继承则导致不该有的功能通过继承而继承了下来,使用接口则代码无法做到最大程度的重用.进而引出设计原则1:找出应用中可能需要变化之 ...