[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树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构 ...
随机推荐
- 几个Google中国的访问IP
前面几个IP的访问速度比较快. 74.125.31.106 173.194.45.20 173.194.45.19 173.194.45.18 173.194.45.17 173.194.45.16 ...
- android SDK更新
在proxy.ini里的[profile]下加上如下配置即可更新android SDK了 dl-ssl.google.com = nofakehttps Oct 26, 2014 #2 2828qw. ...
- 旨在脱离后端环境的前端开发套件 - IDT之Server篇
IDT,一个基于Nodejs的,旨在脱离后端环境的前端开发套件,目的就是能让前端开发完全脱离后端的环境,无论后端是什么模板引擎(主流),都能应付自如. IDT主要包括两大部分:Server + Bui ...
- CSS 知识积累
一.关于定位 1.相对定位下,不需要设定块级元素的宽度,只需要设定高度,即可以使该块级元素自适应,如果内有固定高度子元素,可以不设高度.而且可以进行top left定位. 2.相对定位下,如果两个兄弟 ...
- 转载 Memcached BinaryProtocol incr指令内存泄露的bug
缘起 最近有个分布式限速的需求.支付宝的接口双11只允许每秒调用10次. 单机的限速,自然是用google guava的RateLimiter. http://docs.guava-libraries ...
- Codeforces 414C Mashmokh and Reverse Operation
题意:给你2^n个数,每次操作将其分成2^k份,对于每一份内部的数进行翻转,每次操作完后输出操作后的2^n个数的逆序数. 解法:2^n个数,可以联想到建立一棵二叉树的东西,比如 2,1,4,3就可以 ...
- 用C#读取,写入ini文件
[DllImport("kernel32.dll")] private static extern bool WritePrivateProfileString(string se ...
- ExtJs之Ext.each
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- ExtJs之Ext.getCmp
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- zoj 3513 Human or Pig 博弈论
思路:P态的所有后继全为H态,第一个格子为P态,第一行和第一列为H态. 代码如下: #include<iostream> #include<cstdio> #include&l ...