hdu1251 tire树入门
2015-08-20 10:14
253 查看
#include<stdio.h> #include<string> #include<map> #include<vector> #include<cmath> #include<stdlib.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int N=1e4+10; const int MOD=1e9+7; int n,m,k; struct node{ node* child[26]; int cnt; node(){ cnt=0; for(int i=0;i<26;i++) child[i]=NULL; } }; node *root=new node; node *p,*newp; void insert(char* s){ p=root; int c; int len=strlen(s)-1; for(int i=0;i<len;i++){ c=s[i]-'a'; if(p->child[c]!=NULL){ p=p->child[c]; p->cnt++; }else{ newp=new node; p->child[c]=newp; p=newp; p->cnt++; } } } int search(char *s){ int len=strlen(s); p=root; for(int i=0;i<len;i++){ int c=s[i]-'a'; if(p->child[c]==NULL) return 0; else p=p->child[c]; } return p->cnt; } int main(){ #ifndef ONLINE_JUDGE freopen("aaa","r",stdin); #endif int T; char str[20]; while(fgets(str,100,stdin),strlen(str)!=1) insert(str); while(~scanf("%s",str)) printf("%d\n",search(str)); return 0; }
相关文章推荐
- 统计学习笔记(3)——k近邻法与kd树
- POJ 3259 Wormholes 【SPFA 判断负环】
- Activity之间传递List<T>数据
- 阿里云Ubuntu重装mysql稳定版,更改数据目录,解决中文乱码
- jQuery动画入门--顺序执行
- JAVA 面向对象( 类和对象)
- hdoj 3952 World Exhibition
- 【MySQL】InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据
- 如何快速学习使用mybatis以及总结
- Failed to read artifact descriptor for org.eclipse.tycho 错误
- Emacs 中的正则替换
- 统计学习笔记(2)——感知机模型
- %1$s %1$d Android string
- 网络流(dinic算法)
- spring支持的websocket
- unrar.dll 使用实例
- Effective Objective-C 2.0 第3条:多用字面量语法,少用与之等价的方法
- BAT及各大互联网公司2014前端笔试面试题--Html,Css篇
- 01-复杂度2. Maximum Subsequence Sum (25)
- Linux做简单路由器