http://acm.hdu.edu.cn/showproblem.php?pid=2846

Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2720    Accepted Submission(s): 1060

Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
 
Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
 
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
 
Sample Input
20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s
 
Sample Output
0
20
11
11
2
 
分析:
 
典型的字典树问题。
 
AC代码:
 
 #include<stdio.h>
#include<iostream>
#include<string.h> using namespace std; struct node
{
int count;
int value;
node *next[];
node()
{
for(int i=;i<;i++)
next[i]=;
count=;
value=;
}
}; void insert(node *&root,char *s,int num)
{
int i=,t=;
node *p=root;
if(!p)
{
p=new node();
root=p;
}
while(s[i])
{
t=s[i]-'a';
if(!p->next[t])
p->next[t]=new node();
p=p->next[t];
if(p->value!=num)
{
p->count++;
p->value=num;
}
// printf("%d\n",p->value);
i++;
}
} int find(node *root,char *s)
{
int i=,t=,ans;
node *p=root;
if(!p)
return ;
while(s[i])
{
t=s[i]-'a';
if(!p->next[t])
return ;
p=p->next[t];
ans=p->count;
i++;
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
char str[];
node *root=NULL;
while(T--)
{
scanf("%s",str);
for(int i=;i<strlen(str);i++)
insert(root,str+i,T+);
}
int Q;
scanf("%d",&Q);
while(Q--)
{
scanf("%s",str);
printf("%d\n",find(root,str));
}
return ;
}
 

hdu 2846 Repository的更多相关文章

  1. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  5. HDU 2846 Repository(字典树)

    字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...

  6. HDU 2846:Repository(Trie)

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...

  7. Repository HDU - 2846 字典树

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

  8. Repository HDU - 2846 (trie)

    题中没给范围 所以控制不好数组范围..不是超内存就是runtime.. 好吧 到了晚上终于调出来数组模拟的了 题意: 求含有某字符段的个数 解析: 把每个字符串遍历一遍 以每个元素为起点建树就好了.. ...

  9. HDU 2846 (AC自动机+多文本匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 题目大意:有多个文本,多个模式串.问每个模式串中,有多少个文本?(匹配可重复) 解题思路: 传统 ...

随机推荐

  1. android studio 出错

    http://blog.csdn.net/dhx20022889/article/details/44919905

  2. 页面瀑布流布局的实现 javascript+css

    先看所谓的瀑布流布局 在不使用瀑布流布局的情况下,当页面要显示不同高度的图片时,会如下面显示 下面的元素总是和最靠近它的元素对齐. 为了使元素能够在我们想要的位置上显示,我们使用绝对定位. 说一下大体 ...

  3. 低功耗蓝牙4.0BLE编程-nrf51822开发(5)-链路层

    链路层定义设备处于状态机中五种状态的一种: (1)旁路状态: 处于此状态下的设备不发送或接收数据,处于其它状态下都可以转到此状态. (2)广告状态: 处于此状态的设备发送广播包或者监听.响应广播包.可 ...

  4. nodejs爬虫

    前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...

  5. 【C】漫谈C语言随机数

    来说说C语言如何产生随机数. 有人会说这不简单?time() + srand() + rand() 3个函数不就OK了吗? 是的,不过,我们还是来看看原理比较好,也就是随机数是如何产生的. 这不无聊. ...

  6. C++ 线程类的一个实现

    .h #ifndef CTHREAD_H_ #define CTHREAD_H_ #include "plat.h" class CThread { public: enum { ...

  7. 算法训练 A+B Problem

     算法训练 A+B Problem   时间限制:1.0s   内存限制:512.0MB      问题描述 输入A,B. 输出A+B. 输入格式 输入包含两个整数A,B,用一个空格分隔. 输出格式 ...

  8. Java学习-026-类名或方法名应用之二 -- 统计分析基础

    前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码 此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调 ...

  9. JMeter学习-003-JMeter与LoadRunner的异曲同工

    本节主要对 JMeter 与 LoadRunner 的优缺点进行概要的总结,若有不足之处,敬请指正,不胜感激! 同时,我也不得不承认,在对 JMeter 和 LoadRunner 进行比较时,我个人的 ...

  10. imx6 otg host support

    本文记录添加imx6 otg host支持的过程. 参考链接 http://www.cnblogs.com/helloworldtoyou/p/6108560.html https://communi ...