题目链接: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. Leetcode#73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

  2. Sencha Touch2 时间轴ListPanel

    直接贴代码 timeline.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...

  3. [geeksforgeeks] Bottom View of a Binary Tree

    http://www.geeksforgeeks.org/bottom-view-binary-tree/ Bottom View of a Binary Tree Given a Binary Tr ...

  4. 企业级账号更新app

    企业级账号 版本更新总结       参考:http://jingyan.baidu.com/article/a3aad71aa5fbfbb1fb0096b1.html 1.打包ipa,plist工具 ...

  5. Topcoder srm 632 div2

    脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...

  6. eclipse 自动 注释

    在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...

  7. Sqli-labs less 50

    Less-50 从本关开始我们开始进行order by stacked injection! 执行sql语句我们这里使用的是mysqli_multi_query()函数,而之前我们使用的是mysqli ...

  8. ASP.NET MVC的处理管线

    原文:http://www.cnblogs.com/fzrain/p/3651693.html 下面开始解释各个部分: 路由模块 1.在ASP.NET MVC处理管线中的第一站就是路由模块.当请求到达 ...

  9. post 方式提交XML文件调用接口

    import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java. ...

  10. C++ 第一次上机作业

    今天完成了C++第一次上机作业,感觉比较简单. 题目: 求2个数或3个正整数中的最大数,用带有默认参数的函数实现. 对3个变量按由小到大顺序排序,要求使用变量的引用. 编写一个程序,用同一个函数名对几 ...