[HIHO1260]String Problem I(trie树)
题目链接:http://hihocoder.com/problemset/problem/1260
n个字符串,m次询问。每次询问给一个字符串,问这个字符串仅可以在一个地方加一个字母。这样操作后与n个字符串中有多少个字符串一样。
trie树维护n个字符串,然后从根节点向下dfs。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef struct Node {
Node *next[];
int cnt;
Node() {
cnt = ;
for(int i = ; i < ; i++) {
next[i] = NULL;
}
}
}Node; void insert(Node *p, char *str) {
for(int i = ; str[i]; i++) {
int t = str[i] - 'a';
if(p->next[t] == NULL) {
p->next[t] = new Node();
}
p = p->next[t];
}
p->cnt++;
} int len, cnt; void dfs(Node *p, char *str, int cur, int flag) {
if(flag > ) return;
if(cur == len && flag == ) {
cnt++;
return;
}
for(int i = ; i < ; i++) {
if(p->next[i]) {
// printf("%c\n", 'a'+i);
if('a' + i == str[cur]) {
dfs(p->next[i], str, cur+, flag);
}
else {
if(flag > ) continue;
dfs(p->next[i], str, cur, flag+);
}
}
}
} void del(Node *root) {
for(int i = ; i < ; i++) {
if(root->next[i] != NULL) {
del(root->next[i]);
}
}
delete root;
} const int maxn = ;
int n, m;
char tmp[maxn]; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d %d", &n, &m)) {
Node *root = new Node();
for(int i = ; i < n; i++) {
scanf("%s", tmp);
insert(root, tmp);
}
for(int i = ; i < m; i++) {
scanf("%s", tmp);
len = strlen(tmp);
cnt = ;
dfs(root, tmp, , );
printf("%d\n", cnt);
}
del(root);
}
return ;
}
[HIHO1260]String Problem I(trie树)的更多相关文章
- HNU 13108 Just Another Knapsack Problem DP + Trie树优化
题意: 给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大.每个模式串不止能用一次. 思路: 多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在 ...
- hdu 5687 Problem C trie树
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Prob ...
- 【Hihocoder】1014 : Trie树
问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...
- [转]数据结构之Trie树
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...
- HDU1247 Hat’s Words 【trie树】
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- HDU 11488 Hyper Prefix Sets (字符串-Trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- Trie树:应用于统计和排序
Trie树:应用于统计和排序 1. 什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构 ...
随机推荐
- Linux 的多线程编程的高效开发经验
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- kerberos+ladp+hadoop-ha 安全认证部署配置
随着hadoop集群里的数据量越来越大,各业务数据都集中了里面,自然要为各业务都要提供数据支持,又希望各业务数据是相对独立安全的,这最时候就需要做安全认证了 hadoop ha 测试集群部署规划 ha ...
- Kafka的coordinator
(基于0.10版本) Group Management Protocol Kafka的coordiantor要做的事情就是group management,就是要对一个团队(或者叫组)的成员进行管理. ...
- 驱动笔记 - Makefile
ifneq ($(KERNELRELEASE),) obj-m := hello.ohello-objs := main.o add.o else KDIR := /lib/modules/2.6.1 ...
- 各大Oj平台介绍
1.题库与网站资源题库-在线提交系统(Online Judge)简介 下面是几个比较大的在线提交系统(OnlineJudge)里面有大量历年的竞赛题目,注册一个ID,然后用自己熟悉的语言(一般有P ...
- C# 对动态编辑的一些学习笔记
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comp ...
- SGU101
Dominoes – game played with small, rectangular blocks of wood or other material, each identified by ...
- LoaderManager使用详解(三)---实现Loaders
这篇文字将介绍Loader<D>类,并且介绍自定义Loader的实现.这是本系列的第三篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders ...
- ubuntu第一次设置root密码
安装ubuntu时,系统让用户创建了一个非root用户,系统启动后使用这个用户,在需要执行超级用户权限的指令时,可以通过sudo来执行.为此我们可以通过这样的方式修改root的密码:dengfei@d ...
- C#中的Delegate
谈C#中的Delegate http://www.cnblogs.com/hyddd/archive/2009/07/26/1531538.html