题意:

给你m个字符串,每一个字符对应一个数字,如下:

2 : a, b, c    3 : d, e, f    4 : g, h, i    5 : j, k, l    6 : m, n, o     7 : p, q, r, s    8 : t, u, v    9 : w, x, y, z

输入n个数字串,问这个数字串可以对应几个字符串

比如ade这个字符串对应的数字串就是233

题解:

用这m个字符串建立一颗字典树,对于每一个节点维护一个变量val,他就代表(从树根到这个节点这一个数字串)有多少个对应的字符串

每次插入的时候记录一下最大值就完了

代码:

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #include <algorithm>
6 using namespace std;
7 typedef struct Trie* TrieNode;
8 char s[130];
9 char v[5005][10];
10 struct Trie
11 {
12 int val;
13 TrieNode next[8];
14 Trie()
15 {
16 val = 0;
17 memset(next,NULL,sizeof(next));
18 }
19 };
20
21 void inserts(TrieNode root,char ss[10])
22 {
23 TrieNode p = root; //建立的字典树是字符串对应的数字串
24 int len=strlen(ss);
25 for(int i=0;i<len;++i)
26 {
27 int temp=s[ss[i]];
28 if(p->next[temp]==NULL) p->next[temp]=new struct Trie();
29 p=p->next[temp];
30 }
31 p->val+=1;
32 }
33
34 int query(TrieNode root,char ss[10])
35 {
36 TrieNode p = root;
37 int len=strlen(ss);
38 for(int i=0;i<len;++i)
39 {
40 int temp=ss[i]-'0'-2;
41 if(p->next[temp]==NULL)
42 {
43 //printf("%d**\n",i);
44 return 0;
45 }
46 else
47 {
48 p=p->next[temp];
49 }
50 }
51 return p->val;
52 }
53
54 void Del(TrieNode root)
55 {
56 for(int i=0 ; i<8 ; ++i)
57 {
58 if(root->next[i])Del(root->next[i]);
59 }
60 delete(root);
61 }
62
63 int main()
64 {
65 s['a']=s['b']=s['c']=0;
66 s['d']=s['e']=s['f']=1;
67 s['g']=s['h']=s['i']=2;
68 s['j']=s['k']=s['l']=3;
69 s['m']=s['n']=s['o']=4;
70 s['p']=s['q']=s['r']=s['s']=5;
71 s['t']=s['u']=s['v']=6;
72 s['w']=s['x']=s['y']=s['z']=7;
73 int t,n,m;
74 char ss[10];
75 scanf("%d",&t);
76 while(t--)
77 {
78 TrieNode root = new struct Trie();
79 scanf("%d %d",&n,&m);
80 for(int i=0 ; i<n ; ++i)
81 {
82 scanf("%s",v[i]);
83 }
84 for(int i=0 ; i<m ; ++i)
85 {
86 scanf("%s",ss);
87 inserts(root,ss);
88 }
89 for(int i=0;i<n;++i)
90 {
91 printf("%d\n",query(root,v[i]));
92 }
93 Del(root);
94 }
95 return 0;
96 }

Intelligent IME HDU - 4287 字典树的更多相关文章

  1. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  2. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  3. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

  5. Chip Factory HDU - 5536 字典树(删除节点|增加节点)

    题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...

  6. hdu 1251 字典树的应用

    这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...

  7. hdu 2896 字典树解法

    #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...

  8. Repository HDU - 2846 字典树

    题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...

  9. Phone List HDU - 1671 字典树

    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...

随机推荐

  1. Linux 安装JDK配置环境(rpm安装和压缩版安装)

    jdk安装 (rpm安装) jdk下载地址: https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html ...

  2. update 表名 set 某列名=now() where user in('user1','user2','user3');

    update 表名  set 某列名=now() where user in('user1','user2','user3');

  3. 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

    如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

  4. SDUST数据结构 - chap9 排序

    判断题: 选择题: 编程题: 7-1 排序: 输入样例: 11 4 981 10 -17 0 -20 29 50 8 43 -5 输出样例: -20 -17 -5 0 4 8 10 29 43 50 ...

  5. CMU数据库(15-445)实验2-B+树索引实现(下+课上笔记)

    4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...

  6. Scalable Go Scheduler Design Doc

    https://docs.google.com/document/d/1TTj4T2JO42uD5ID9e89oa0sLKhJYD0Y_kqxDv3I3XMw/ Scalable Go Schedul ...

  7. Servlet中的一些注意事项

    servlet中的一些注意事项 1 什么是servlet? 1)Servlet是Sun公司制定的一套技术标准,包含与Web应用相关的一系列接口,是Web应用实现方式的宏观解决方案.而具体的Servle ...

  8. YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. data = yaml.load(file_data)

    YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsa ...

  9. Win10家庭版Hyper-V出坑(完美卸载,冲突解决以及Device Guard问题)

    本文链接:https://blog.csdn.net/hotcoffie/article/details/85043894 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附 ...

  10. DPDK CAS(compare and set)操作

    前言 rte_ring是一个无锁队列,无锁队列的出队入队操作是rte_ring实现的关键.因此,本文主要讲解dpdk是怎样使用无锁机制实现rte_ring的多生产者入队操作. rte_atomic32 ...