[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树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构 ...
随机推荐
- 【BZOJ】【3856】Monster
又是一道水题…… 重点是分情况讨论: 首先我们很容易想到,如果a*k-b*(k+1)>0的话那么一定能磨死Monster. 但即使不满足这个条件,还有可能打死boss: 1.h-a<1也就 ...
- Leetcode#99 Recover Binary Search Tree
原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...
- 查看windows系统热键占用情况
有时候我们经常用一些软件中的快捷键,但是会发现快捷键设置的很正确,但是就是不起作用.这就是因为这些快捷键被系统或者其他软件占用了. 那么这时我们怎么知道是哪个软件占用了呢?这确实是个纠结的问题,还好大 ...
- 响应式嵌入 iframe Pym.js
Pym.js 可以让你在嵌入 iframe 的时候可自动的对 iframe 的大小进行调整以适应父一层容器,并且可以避免跨域问题. 支持浏览器: Internet Explorer 9, 10 (Wi ...
- Hadoop 系统配置 map 100% reduce 0%
之前在本地配置了hadoop伪分布模式,hdfs用起来没问题,mapreduce的单机模式也没问题. 今天写了个程序,想在伪分布式上跑一下mapreduce,结果出现 map 100% reduce ...
- spring 两个 properties
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件: A模块 A模块的Spring配置文件如下: <?xml version="1.0" enc ...
- HDU 2843 I Will Win(概率题?,怨念颇深,简单)
题目 真不想说什么,,,这神题真讨厌,,,多校的.. //又是一道神题... #include<stdio.h> #include<string.h> //最大公约数 int ...
- css display visibility
当visibility被设置为"hidden"的时候,元素虽然被隐藏了,但它仍然占据它原来所在的位置.注意,当元素被隐藏之后,就不能再接收到其它事件了. display属性就有一点 ...
- awk处理之案例一:awk 处理百分比的问题
编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...
- hdu 4023 Game 博弈论
思路: 将15种分成5类: 1.1和2为一类: 2.3,4,5,6为一类: 3.7,8,9,10为一类: 4.11,12,13,14,15为一类: 5.15为一类. 比较各类的优先级,就会发现放置的顺 ...