hihocode 1014 Trie树
2016-04-22 22:16
513 查看
前些日子清明节。。 无聊,自学了字典树的皮毛
首先是仓鼠学长在上学期给了我一个500多行的字典树代码。。
然后似乎是队长大人调侃说我可以在70行里面完成。。
这个梗一直记着。。 500多行的代码倒是丢了。。
于是就稍微看了一下最基础的。。
感觉这种方式空间占用好大。。
下面给出模板,我太懒把test写成全局了。。 指针只要稍微改一下就好了。。
唔。。明天省赛,和江一起打镜像,我要好好表现啦!
首先是仓鼠学长在上学期给了我一个500多行的字典树代码。。
然后似乎是队长大人调侃说我可以在70行里面完成。。
这个梗一直记着。。 500多行的代码倒是丢了。。
于是就稍微看了一下最基础的。。
感觉这种方式空间占用好大。。
下面给出模板,我太懒把test写成全局了。。 指针只要稍微改一下就好了。。
唔。。明天省赛,和江一起打镜像,我要好好表现啦!
#include<cstdio> #include<iostream> #include<cstring> #include<string> #include<algorithm> using namespace std; string test; struct trie { trie *next[26]; int v; trie() { v=0; for(int i=0;i<26;i++) { next[i]=NULL; } } }; trie root; void build() { int len=test.length(); trie *p=&root; for(int i=0;i<len;i++) { int id=test[i]-'a'; if(p->next[id]==NULL) { trie *q=new trie; p->next[id]=q; } p=p->next[id]; p->v++; } } int find() { int len=test.length(); trie *p=&root; for(int i=0;i<len;i++) { int id=test[i]-'a'; if(p->next[id]==NULL) { return 0; } p=p->next[id]; } return p->v; } int main() { int n; cin>>n; for(int i=1;i<=n;i++) { test.clear(); cin>>test; build(); } int m; cin>>m; for(int i=1;i<=m;i++) { test.clear(); cin>>test; int ans=find(); cout<<ans<<endl; } return 0; }
相关文章推荐
- 设计模式之行为型模式 - 调用行为的传递问题
- [div+css]晒晒最新制作专题推广页模板
- 2008大学生入党申请书 模板
- IMAIL多语言模板两套Outlook&Gmail模板下载
- 字典树的基本知识及使用C语言的相关实现
- 在PHP中使用模板的方法
- 深入解析php模板技术原理【一】
- Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中
- 在ASP中不用模板生成HTML静态页直接生成.html页面
- 基于HTML模板和JSON数据的JavaScript交互(移动端)
- C#模板方法模式(Template Method Pattern)实例教程
- javascript文本模板用法实例
- 关于Asp代码与页面的分离模板技术第1/3页
- php模板原理讲解
- 需要使用php模板的朋友必看的很多个顶级PHP模板引擎比较分析
- DataGrid 动态添加模板列 实现代码
- 详解java模板和回调机制
- C++模板之特化与偏特化详解
- vs.net2008添加模板方法
- ThinkPHP模板判断输出Empty标签用法详解