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
}
查看全文
相关阅读:
C语言枚举类型使用简介
C实现单链表(转)
不同数据库数据类型
Informix 常用命令
工作了
修改route使用有线/无线同时连接内外网
Perl 时间函数
Linux 系统命令
SQL SERVER 触发器示例
Informix 函数
原文地址:https://www.cnblogs.com/Knuth/p/1562612.html
最新文章
linux下调试python程序
python threadpool使用注意事项
synflood测试
MySQL 5.1/5.5 WiNDOWS REMOTE R00T (mysqljackpot) 验证windows下mysql提权
深入解析无线WEP和WPA密码及破解原理
linux shell审计snoopy的注意事项
sublime编辑器
[转载]一个简明Erlang指南
20121117 回归到写博客
阅读源码学习erlang:erlang_websocket (1) websocket_server.erl
热门文章
XCode5.0.1 修改资源文件之后 运行程序,资源文件不自动更新问题
python .plist 文件读写
cocos2dx 图片拼接出现缝隙或者黑边问题解决方案
Linux下Socket编程
字符数组 字符指针 sizeof strlen 的区别
c语言 位操作
指向结构体指针的例子
Linux下常用函数 信号处理函数(转)
Linux进程间共享临界区“信号量”编程
Linux 守护进程的编程方法(转)
Copyright © 2011-2022 走看看