zoukankan
html css js c++ java
字典树 模板
Code
1
//
字典树模板
2
#include
<
iostream
>
3
using
namespace
std;
4
const
int
kind
=
26
;
5
struct
node
6
{
7
int
i,count;
8
node
*
next[kind];
9
node()
10
{
11
count
=
1
;
12
for
(i
=
0
;i
<
kind;i
++
)
13
next[i]
=
NULL;
14
}
15
}
;
16
void
insert(node
*
root,
char
*
word)
17
{
18
int
i
=
0
,branch;
19
node
*
local
=
root;
20
if
(local
==
NULL)
21
{
22
local
=
new
node();
23
root
=
local;
24
}
25
while
(word[i])
26
{
27
branch
=
word[i]
-
'
a
'
;
28
if
(local
->
next[branch])
29
local
->
next[branch]
->
count
++
;
30
else
31
local
->
next[branch]
=
new
node();
32
i
++
;
33
local
=
local
->
next[branch];
34
}
35
}
36
int
search(node
*
root,
char
*
word)
37
{
38
int
i
=
0
,branch,ans;
39
node
*
local
=
root;
40
41
if
(local
==
NULL)
42
return
0
;
43
while
(word[i])
44
{
45
branch
=
word[i]
-
'
a
'
;
46
if
(
!
local
->
next[branch])
47
return
0
;
48
i
++
;
49
local
=
local
->
next[branch];
50
ans
=
local
->
count;
51
}
52
return
ans;
53
}
54
int
main()
55
{
56
char
str[
12
];
57
node
*
root
=
new
node;
58
while
(gets(str)
&&
strcmp(str,
""
))
59
insert(root,str);
60
while
(scanf(
"
%s
"
,str)
!=
EOF)
61
printf(
"
%d\n
"
,search(root,str));
62
return
0
;
63
}
查看全文
相关阅读:
一、操作m'y's'ql
十三、并发
十二、异步_实践
一、数据
【2019-08-13】琐碎事才是突破
【2019-08-12】迟到好过没到
【2019-08-11】别人约我宵夜,我却约人早茶
【2019-08-10】习惯跟时间有关
【2019-08-09】一日之计在于晨
【2019-08-08】少即是多,慢即是快
原文地址:https://www.cnblogs.com/Knuth/p/1562612.html
最新文章
Codevs1403 新三国争霸
Uva1401 Remember the Word
POJ1276 Cash Machine
洛谷P1710地铁涨价
洛谷P1371 NOI元丹
Ubuntu Server 16.04 LTS上怎样安装下载安装Nginx并启动
DataGridView中在新增行时怎样设置每个Cell单元格的字体样式
DataGridView中获取与设置当前选中行以及SelectedRows和CurrentRow注意区分
DataGridView中的rows.Count比实际行数多1的原因以及解决办法
DataGridView怎样实现添加、删除、上移、下移一行
热门文章
DevExpress的TextEdit控件没法调整高度解决
DevExpress的分页Tab控件XtraTabControl控件的使用
Nginx入门简介和反向代理、负载均衡、动静分离理解
VS中怎样对C#项目进行单元测试
大数据
一、MySql 对JSON的支持
三、SQL Server 对JSON的支持
前端JSON添加
一、查看MVC4还是MVC5
一、Subversion服务
Copyright © 2011-2022 走看看