题目链接: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树)的更多相关文章

  1. HNU 13108 Just Another Knapsack Problem DP + Trie树优化

    题意: 给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大.每个模式串不止能用一次. 思路: 多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在 ...

  2. hdu 5687 Problem C trie树

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Prob ...

  3. 【Hihocoder】1014 : Trie树

    问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...

  4. [转]数据结构之Trie树

    1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...

  5. HDU1247 Hat’s Words 【trie树】

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  7. 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 ...

  8. POJ 3630 Phone List(trie树的简单应用)

    题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...

  9. Trie树:应用于统计和排序

    Trie树:应用于统计和排序 1. 什么是trie树 1.Trie树 (特例结构树)       Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构 ...

随机推荐

  1. 从Theano到Lasagne:基于Python的深度学习的框架和库

    从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...

  2. __cdecl __stdcall __fastcall之函数调用约定讲解

    首先讲解一下栈帧的概念: 从逻辑上讲,栈帧就是一个函数执行的环境:函数参数.函数的局部变量.函数执行完后返回到哪里等等. 实现上有硬件方式和软件方式(有些体系不支持硬件栈) 首先应该明白,栈是从高地址 ...

  3. KMP_Best Reward

    大意:把一个字符串分成两串,假如一个字符串是回文串就可以加上它的VALUE,否则它的VALUE为0: KMP的特点是可以求出前缀与后面的字符串是否匹配, 注意回文串的特点,所以当我们把回文串反转的时候 ...

  4. HDOJ1050

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. Node.js NPM国内镜像

    NPM国内镜像 http://npm.hacknodejs.com/ http://registry.npmjs.vitecho.com/ https://registry.npm.taobao.or ...

  6. POJ 2531 Network Saboteur (枚举+剪枝)

    题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大. 目前我所知道的有四种做法: 方法一:状态压缩 #include <iostream> #inc ...

  7. HDU 1260 Tickets(简单dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. hdu 4155 The Game of 31 博弈论

    给出序列,在剩下的卡中选择,谁先拿到大于31的输,搜一下就可以了! 代码如下: #include<cstdio> #include<cstring> ]; ],sum; boo ...

  9. linux下文件编码的查看与修改

    在Linux中查看文件编码可以通过vim编辑器来查看,在vim命令模式下输入如下命令即可: :set fileencoding //在vim中查看文件编码 如果你只是想查看其它编码格式的文件或者想解决 ...

  10. cocos2d-x3.0环境搭建(基于win7以及mac)

    流程概览: Windows平台 一.安装 Python与配置Python环境变量 二.安装Cocos2d-x,并创建项目 Mac平台 安装Cocos2d-x,并创建项目 具体操作: 一.安装Pytho ...