HDU 1251 统计难题
2015-08-15 18:32
357 查看
注意用g++交,否则会超内存
#include<iostream> #include<cstdio> #include<cstring> #define maxn 26 using namespace std; struct node { int cnt; node * next[maxn]; node() { cnt=0; memset(next,0,sizeof(next)); } }; node *p,*root=new node(); void insert(char *s) { p=root; for(int i=0;s[i];i++) { int id=s[i]-'a'; if(p->next[id]==NULL) p->next[id]=new node(); //没有就新建节点 p=p->next[id];//当前节点 p->cnt++;//出现次数加一 } } int query(char *s) { p=root; for(int i=0;s[i];i++) { int id=s[i]-'a'; if(p->next[id]==NULL) return 0; p=p->next[id]; } return p->cnt; } int main() { char s[20]; while(gets(s),*s) insert(s); while(gets(s)) cout<<query(s)<<endl; }
相关文章推荐
- Android中的Selector
- 删除从Mac上面下载来的图片文件
- Hibernate直接执行SQL语句
- 谁,例如下列方法区的指导下
- 树莓派用Python写几个简单程序4_UART
- 最小生成树之kruskal算法
- socket关闭 四次握手
- 重定向?传值
- Java并发编程实践读书笔记
- 文章标题
- POJ1064 二分
- Iterator理解
- 高斯消元模版
- 【学习Egret】第一个H5小游戏,人人来打产品汪
- URAL1671 Anansi's Cobweb(离线做 + 并查集)
- HCDJPM学习随记
- 回溯法——Red and Black
- iOS Auto Layout学习
- URAL1671 Anansi's Cobweb(离线做 + 并查集)
- C++引用详解