题目链接:http://hihocoder.com/problemset/problem/1014

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e6 + ; // 1e5 * 10 (1e5个单词 * 单词长度 (<= 10))
const int N = ; int le;
char str[N]; struct Trie
{
int cnt;
int next[N];
void init() {
cnt = ;
memset(next, -, sizeof(next));
}
}T[maxn]; void Build(char *s)
{
int i = , p = ;
while (s[i]) {
int x = s[i] - 'a';
if (T[p].next[x] == -) {
T[le].init();
T[p].next[x] = le++;
// printf("T[%d].next[%d] = %d, ", p, x, T[p].next[x]);
}
p = T[p].next[x];
T[p].cnt++;
// printf("T[%d].cnt = %d\n", p, T[p].cnt);
i++;
}
} void Query(char *s)
{
int i = , p = ;
while (s[i]) {
int x = s[i] - 'a';
if (T[p].next[x] == -) {
puts("");
return ;
}
p = T[p].next[x];
i++;
}
printf("%d\n", T[p].cnt);
} int main()
{
int n, m;
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &n) != EOF) {
le = ;
T[].init(); // !root
while (n--) {
scanf("%s", str);
Build(str);
}
scanf("%d", &m);
while (m--) {
scanf("%s", str);
Query(str);
}
}
return ;
}

hiho一下第二周 Trie树的更多相关文章

  1. hiho一下 第二周 trie树

    Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路 ...

  2. hiho一下 第二周&第四周:从Trie树到Trie图

    hihocoder #1014 题目地址:http://hihocoder.com/problemset/problem/1014 hihocoder #1036 题目地址: http://hihoc ...

  3. hiho一下21周 线段树的区间修改 离散化

    离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho ...

  4. hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)

    思路: 完全看题目中的介绍就行了.还有里面的input写道:不保证是英文单词,也有可能是火星文单词哦.比赛结束后的提交是不用考虑26个字母之外的,都会AC,如果考虑128种可能的话,爆了内存.步骤就是 ...

  5. 【hiho一下第二周 】Trie树

    [题目链接]:http://hihocoder.com/problemset/problem/1014 [题意] [题解] 在字典树的域里面加一个信息cnt; 表示这个节点下面,记录有多少个单词; 在 ...

  6. hiho一下20周 线段树的区间修改

    线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了 ...

  7. CCF NOI Online 2021 提高组 T3 岛屿探险(CDQ 分治,Trie 树)

    题面 凇睦是一个喜欢探险的女孩子,这天她到一片海域上来探险了. 在这片海域上一共有 n 座岛屿排成一排,标号为 1, 2, 3, . . . , n.每座岛屿有两个权值,分别为劳累度 ai 和有趣度 ...

  8. HihoCoder第二周与POJ3630:Trie树的建立

    这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...

  9. hiho 第二周

    Trie树,第一次写,简单的建树+搜索 它的思路hiho上讲得很清楚,good~ #include<iostream> #include<string> using names ...

随机推荐

  1. linux网络:常用命令(二)

    1.ping命令:与Windows下基本功能一样但也有所区别 1.命令格式: ping [选项] ip地址或域名主机等 选项 -c 加次数 -d 使用Socket的SO_DEBUG功能. -f 极限检 ...

  2. CF464A (模拟)

    http://codeforces.com/contest/465/problem/C Codeforces Round #265 (Div. 2) C Codeforces Round #265 ( ...

  3. Linux下安装和配置JDK与Tomcat(入门版)

    JDK路径:/usr/java/jdk1.6.0_25 Tomcat路径:/usr/local/apache-tomcat 1. 下载jdk6.0(选择“.rpm.bin”结尾的,6u25版本) ht ...

  4. Windows下几个常用的和进程有关的命令

    在windows下,进入cmd,有几个常用的和进程有关的命令: netstat -ano:查看所有进程 netstat -ano|findstr  “端口号”:查看端口号对应的进程PID taskli ...

  5. springmvc之默认错误页面跳转

    在做一个项目的时候,为了界面美观及用户体验,我们往往会设计自己的错误跳转页面,而不是直接展示给用户一堆错误码,为此我们需要配置自己的错误跳转页面. 1.项目结构 2.web.xml <!DOCT ...

  6. HDU 2795

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2795 线段树问题,线段树的每个叶子节点保存的是当前行还剩余的长度,每次查询找到满足条件的一行减去该条幅的 ...

  7. jquery 平滑锚

    setTimeout('$("html,body").animate({ scrollTop: $(".title").offset().top }, 1000 ...

  8. EditorGUILayout.EnumPopup 枚举弹出选择菜单

    http://www.unity蛮牛.com/thread-25490-1-1.html http://www.unity蛮牛.com/m/Script/EditorGUILayout.EnumPop ...

  9. linux各文件夹的作用

    文章转载自:http://www.cnblogs.com/amboyna/archive/2008/02/16/1070474.html 精------------------------------ ...

  10. 17.3---阶乘尾多少个0(CC150)

    思路,其实这题easy,就是看多少个5. 答案: public static int getFactorSuffixZero(int n) { // write code here int res = ...