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
}
查看全文
相关阅读:
openssh升级
Mysql基础学习_Windows版(一)
centos7 取消Ctrl+Alt+Del重启功能
linux shell数值比较和字符串比较
linux 使用中括号进行条件判断
VXLAN简介(摘抄)
centos配置NTP服务器
Centos配置网卡子接口
组播___IGMP
组播
原文地址:https://www.cnblogs.com/Knuth/p/1562612.html
最新文章
查找cordova插件name
最新解决VS2017+ Mysql + EF 创建实体数据模型 闪退的办法
树莓派研究笔记(8)-- 编译lakka v2.1源码
Json Post到 https的坑
在windows中安装OpenSSH,无密码登录,永远不断线
树莓派研究笔记(10)-- Retropie 模拟器
树莓派研究笔记(9)-- 树莓派SPI连接TFT屏幕
树莓派研究笔记(8)-- 编译lakka v2.1源码
树莓派研究笔记(7)-- lakka 《仙剑奇侠传》的完美移植
一句话设计模式
热门文章
【VUE】响应式原理笔记1
【VUE】Class 与 Style 绑定
【VUE】计算属性:getter与setter
【工作经验】解决报错:SyntaxError: Unexpected token .
讲个寓言
【工作经验】WT新项目2:进度紧张分析
【UML系列】一张图讲透UML类图
【技术方案】在外部网址调试本地js(基于fiddler)
【源码】JS工具对象系列1:URL工具对象
【源码】JQuery源码:代码结构、模块依赖
Copyright © 2011-2022 走看看