4189 字典

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 大师 Master
 查看运行结果
 
 
题目描述 Description

最经,skyzhong得到了一本好厉害的字典,这个字典里整整有n个单词(1<=n<=200000)

现在skyzhong需要在字典里查询以某一段字母开头的单词

如:skyzhong想查询a

那么只要是a开头的单词就可以了

skyzhong只想知道里面有没有这一个单词(因为没有他就不查了)

若有,请输出YES。若没有,请输出NO

输入描述 Input Description

第一行一个数n

第二行到第n+1行,一行一个字符串

再下一行一个数m,表示skyzhong想要查询的次数

接着m行,一行一个字符串,表示skyzhong想要查的东西

输出描述 Output Description

共m行,若有这字串输出YES,否则输出NO

样例输入 Sample Input

3

asd

asfdghj

asfd

3

asd

asdghj

asf

样例输出 Sample Output

YES

NO

YES

数据范围及提示 Data Size & Hint

字符串只有小写字母,且长度≤8

思路:

  裸tire树;

来,上代码:

#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; struct TreeNode {
bool if_[]; TreeNode *word[];
}; class TireTreeType {
private:
TreeNode *null,*root; inline void Insert_Tire(TreeNode *now,char str[])
{
int len=strlen(str);
for(int i=;i<len;i++)
{
if(!now->if_[str[i]-'a'])
{
now->if_[str[i]-'a']=true;
now->word[str[i]-'a']=new TreeNode;
for(int j=;j<;j++) now->word[str[i]-'a']->word[j]=null;
}
now=now->word[str[i]-'a'];
}
} inline bool Find_Tire(TreeNode *now,char str[])
{
int len=strlen(str);
for(int i=;i<len;i++)
{
if(now->if_[str[i]-'a'])
{
now=now->word[str[i]-'a'];
}
else return false;
}
return true;
} public:
TireTreeType()
{
null=new TreeNode;
for(int i=;i<;i++) null->word[i]=null;
root=new TreeNode;
for(int i=;i<;i++) root->word[i]=null;
} void Insert(char str[])
{
Insert_Tire(root,str);
} bool Find(char str[])
{
return Find_Tire(root,str);
}
};
class TireTreeType tree; int n,m; char Cgets[]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",Cgets);
tree.Insert(Cgets);
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%s",Cgets);
if(tree.Find(Cgets)) printf("YES\n");
else printf("NO\n");
}
return ;
}

AC日记——字典 codevs 4189的更多相关文章

  1. AC日记——楼房 codevs 2995

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 地平线(x轴)上有n个矩(lou ...

  2. AC日记——传话 codevs 1506 (tarjan求环)

    1506 传话  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 一个朋友网络,如果a认识b,那么如果a第 ...

  3. AC日记——绿色通道 codevs 3342

    3342 绿色通道  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description <思远高考绿色通道&g ...

  4. AC日记——蓬莱山辉夜 codevs 2830

    2830 蓬莱山辉夜  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在幻想乡中,蓬莱山辉夜是月球 ...

  5. AC日记——苹果树 codevs 1228

    1228 苹果树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 在卡卡的房子外面,有一棵 ...

  6. AC日记——刺激 codevs 1958

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold     题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺激(exitement,由于刺激 ...

  7. AC日记——红与黑 codevs 2806

    2806 红与黑  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 有一个矩形房间,覆盖正方形瓷 ...

  8. AC日记——热浪 codevs 1557 (最短路模板题)

    1557 热浪  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 德克萨斯纯朴的民眾们这个夏 ...

  9. AC日记——开关灯 codevs 1690

    开关灯 思路: 线段树: bool懒标记维护: 更新区间时是区间总值减去当前值: 来,上代码: #include <cstdio> #include <cstring> #in ...

随机推荐

  1. Lua 5.2 Reference Manual

    Lua 5.2 Reference Manual.pdf

  2. css-缩写

    border缩写 /*缩写前*/ element{ border-top-width:1px; border-top-style:solid; border-top-color:#cccccc; } ...

  3. OWIN产生的背景以及简单介绍

    OWIN产生的背景以及简单介绍 随着VS2013的发布,微软在Asp.Net中引入了很多新的特性,比如使用新的权限验证模块Identity, 使用Async来提高Web服务器的吞吐量和效率等.其中一个 ...

  4. Android项目--浅析系统通讯录中的那些方法

    系统通讯录,以前的版本虽然过时了,不过有些东西还是可以用. 1.开启系统联系人添加 /** 添加联系人 */ Intent intent = new Intent(Intent.ACTION_INSE ...

  5. html5 canvas+js实现ps钢笔抠图

    html5 canvas+js实现ps钢笔抠图 1. 项目要求需要用js实现photoshop中钢笔抠图功能,就用了近三四天的时间去解决它,最终还是基本上把他实现了. 做的过程中走了不少弯路,最终一同 ...

  6. SpringMVC中Controller

    详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析] 目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodR ...

  7. MVC 5 的 EF6 Code First 入门 系列:排序、筛选和分页

    这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第三篇:排序.筛选 ...

  8. Algorithm lecture

    当前标签: lecture   组合数求法讲解 BLADEVIL 2014-01-08 18:59 阅读:3 评论:0     mobius反演讲解 BLADEVIL 2014-01-08 18:13 ...

  9. Apache + Tomcat +mod_jk 实现集群

    实现效果:用apache 分发请求到tomcat中的对应的项目 环境说明: 操作系统:window7 Javasdk: 1.7 Apache: 2.2.14    (本地安装路径:D:\Apache2 ...

  10. (转)C++重写、重载和重定义的区别

    C++ 重写重载重定义区别 (源自:http://blog.163.com/clevertanglei900@126/blog/static/111352259201102441934870/) 1 ...