统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 34909    Accepted Submission(s): 13109

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).

 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.

 
Sample Input
banana
band
bee
absolute
acm
 
ba
b
band
abc
 
Sample Output
2
3
1
0
 

题目链接:HDU 1251

以前以为很高级的数据结构,原来就是一个很多后继节点的链表而已,建立过程很简单明了,不懂的话画一个多叉树就知道了……,每一次从表头*root(最近学的是链表,就用*L好了)开始找,然后如果存在就继续找直到遍历完字符串,插入和查找都是这个原理还有最好别用G++提交容易爆内存,C++就不会了

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=26;
const int M=15;
struct Trie
{
Trie *nxt[N];
int cnt;
Trie()
{
CLR(nxt,0);
cnt=0;
}
};
Trie *L=new Trie();
void update(char s[])
{
int len=strlen(s);
int indx;
Trie *cur=L;
for (int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(cur->nxt[indx])
{
cur=cur->nxt[indx];
++(cur->cnt);
}
else
{
Trie *one=new Trie();
++(one->cnt);
cur->nxt[indx]=one; cur=cur->nxt[indx];///新建之后还是要跳到这个新建节点的
}
}
}
int Find(char s[])
{
int len=strlen(s);
Trie *cur=L;
int indx;
for(int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(!cur->nxt[indx])
return 0;
cur=cur->nxt[indx];
}
return cur->cnt;
}
char s[M];
int main(void)
{
while (gets(s)&&strcmp(s,""))
update(s); while (gets(s))
printf("%d\n",Find(s));
return 0;
}

HDU 1251 统计难题(Trie模版题)的更多相关文章

  1. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

  2. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  3. hdu 1251 统计难题(trie树入门)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  4. HDU - 1251 统计难题(trie树)

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  5. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  6. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  7. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  8. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. HDU 1251 统计难题 (字符串-Trie树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

随机推荐

  1. AJAX,JSON搜索智能提示

    效果 开发结构参考AJAX,JSON用户校验 主要有两个核心文件 1,处理输入字符,进行后台搜索的servlet Suggest.java package org.guangsoft.servlet; ...

  2. svn 结合rsync 的代码发布系统

    由开发提交到测试环境,经测试,在由运维统一上线.试验需求一台测试服务器,一台线上(生产环境)服务器.测试服务器上跑svn是开发用于代码管理,而线上跑的svn是运维用来代码上线的.结合rsync保持测试 ...

  3. OneApm

    cloudinsight-java-sdk https://github.com/cloudinsight

  4. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  5. blend 从无到有系列之添加自定义Rectangle样式指定到资源文件

    相关链接 http://www.cnblogs.com/wildfeng/archive/2012/03/30/2425248.html http://www.cnblogs.com/jv9/arch ...

  6. 兼容古董级IE小结

    IE6已经死亡,当然7,8,9,10也挂掉了.微软对IE11更下了狠手,对其停止了更新.以为前端就可以安安心心地写代码了.可是就是有些顽固分子,竟然用的还是IE6,尊崇客户至上的原则,就恶心着给他兼容 ...

  7. uva 562

    Description   It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fight ...

  8. SU Demos-03T-F Analysis-01Sugabor

    先看readme, 运行结果,

  9. js:数据结构笔记2---列表

    列表: 定义:一组有序的数据: function List() { this.listSize = 0; this.pos = 0; this.dataStore = []; this.find = ...

  10. 水题 ZOJ 3876 May Day Holiday

    题目传送门 /* 水题:已知1928年1月1日是星期日,若是闰年加1,总天数对7取余判断就好了: */ #include <cstdio> #include <iostream> ...