统计难题

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

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
 
Sample Output
2
3
1
0
 
Author
Ignatius.L
 
Recommend
Ignatius.L
     字典树.....
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h> typedef struct trie
{
//由于只有小写字母a~z-->26
struct trie *child[];
int deep; //--->相似的程度
}Trie; /*作为一个头指针*/ Trie *root; void Insert(char *a)
{
int len,i;
Trie *current, *creatnew;
len=strlen(a);
if(len)
{
current=root;
for(i=;i<len;i++)
{
if(current->child[a[i]-'a']!=)
{
current=current->child[a[i]-'a'];
current->deep=current->deep+;
}
else
{
creatnew=(Trie *)malloc(sizeof(Trie));
for(int j=;j<;j++)
{
creatnew->child[j]=;
}
current->child[a[i]-'a']=creatnew;
current=creatnew;
current->deep=;
}
}
}
} int Triefind(char *a)
{
int i,len;
Trie *current;
len=strlen(a);
if(!len) return ;
current=root;
for(i=;i<len;i++)
{
if(current->child[a[i]-'a']!=)
{
current=current->child[a[i]-'a'];
}
else
return ;
}
return current->deep;
} int main()
{
int i;
char temp[];
root=(Trie *)malloc(sizeof(Trie));
for(i=;i<;i++)
{
root->child[i]=;
}
root->deep=;
while(gets(temp),strcmp(temp,""))
Insert(temp);
memset(temp,'\0',sizeof(temp));
while(~scanf("%s",temp))
printf("%d\n",Triefind(temp));
free(root);
return ;
}

模板:

代码:

 /*hdu 1251 字典树之统计*/
#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct Trie
{
struct Trie *child[];
int deep;
}trie; int idx(char s){
return s-'a';
}
void Insert(char *s,trie *root)
{
trie *cur,*newcur;
cur=root;
int i,j;
for(i=;s[i]!='\0';i++)
{
if(cur->child[idx(s[i])]==NULL) //为空指针
{
newcur=(trie *)malloc(sizeof(trie));
newcur->deep=;
for(j=;j<;j++)
newcur->child[j]=NULL; //设置为空指针
cur->child[idx(s[i])]=newcur;
}
cur=cur->child[idx(s[i])]; //向下推一层
cur->deep+=; //层数加一
}
}
int query(char *s,trie *root)
{
trie *cur=root;
int i;
for(i=;s[i]!='\0';i++){
if(cur->child[idx(s[i])]!=NULL)
cur=cur->child[idx(s[i])];
else
return ;
}
int tem=cur->deep;
return tem;
}
void del(trie *root)
{
int i=;
for(i=;i<;i++){
if(root->child[i]!=NULL)
del(root->child[i]);
}
free(root);
}
char ss[];
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
trie *root=(trie *)malloc(sizeof(trie));
for(int i=;i<;i++)
root->child[i]=NULL; //设置为空指针
while(gets(ss),strcmp(ss,""))
Insert(ss,root);
while(scanf("%s",ss)!=EOF)
printf("%d\n",query(ss,root));
del(root);
return ;
}

HDUOJ-----(1251)统计难题的更多相关文章

  1. hduoj 1251 统计难题

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

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

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

  3. HDU 1251 统计难题 (Trie)

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

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

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

  5. HDU 1251 统计难题(Trie模版题)

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

  6. HDU 1251统计难题

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

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

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

  8. hdu 1251 统计难题 trie入门

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

  9. [ACM] hdu 1251 统计难题 (字典树)

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

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

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

随机推荐

  1. python里的引用、浅拷贝、深拷贝

    参考: 1.http://wsfdl.com/python/2013/08/16/%E7%90%86%E8%A7%A3Python%E7%9A%84%E6%B7%B1%E6%8B%B7%E8%B4%9 ...

  2. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  3. Mysql 监控 支持 mysql 多实例自动发现以及主从监控

    在[/usr/local/zabbix327/bin] 目录下新建python文件,并增加执行权限,如下: #!/usr/bin/env /usr/bin/python # _*_ coding:ut ...

  4. C#一个FTP操作封装类FTPHelper

    参考了网上一些代码,作了一些调整优化. 001 using System; 002 using System.Collections.Generic; 003 using System.Linq; 0 ...

  5. vue-router登录校验后跳转到之前指定页面如何实现

    两个需求:1.用户点击购买需要下单,并跳转到订单页面,但是如果用户没有登录的话,中间有登录验证,会拦截:2.点击购买的时候,登录校验成功了,跳转到订单页面时,订单已创建,去付款即可.3.处理拦截至登录 ...

  6. IOS info.plist配置文件

    创建ios程序时,系统会自动生成一个info.plist文件,它是一个必不可少的文件,因为在这个文件中,存放是应用程序的配置信息,比如本地化语言.版本号.软件名称等,当然,我们也可以在项目的属性中进行 ...

  7. Cognos Report Studio 链接查询需要注意的地方2

    在Report Studio里面用SQL设计报表,查询2,查询3 要链接一般按条件  a1=b1 在选择链接方式需要注意的地方: 默认链接 外部链接 需要设置打开FM,打开报表设计引用的数据包(FM- ...

  8. 我所认识的PCA算法的princomp函数与经历 (基于matlab)

    我接触princomp函数,主要是因为实验室的项目需要,所以我一接触的时候就希望快点学会怎么用. 项目中需要利用PCA算法对大量数据进行降维. 简介:主成分分析 ( Principal Compone ...

  9. 微信小程序开发:设置消息推送

    开发设置中,启用并设置消息推送配置后,用户发给小程序的消息以及开发者需要的事件推送,都将被微信转发至该服务器地址中. 不过照着说明去操作,即使按照最简单的明文方式去设置,还是提示Token验证失败.仔 ...

  10. 基于XMPP 协议的开发 android

    设计过一款基于开源的XMPP即时通信协议的软件.採用C/S协议,通过GPRS无线网络用TCP协议到server.以架设开源的Openfire server作为即时通讯平台 系统主要由下面部分组成:一是 ...