HDUOJ-----(1251)统计难题
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 14434 Accepted Submission(s): 6219
注意:本题只有一组测试数据,处理到文件结束.
- #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)统计难题的更多相关文章
- hduoj 1251 统计难题
http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
- HDU 1251 统计难题 (Trie)
pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/ ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 1251 统计难题(Trie模版题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251统计难题
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- [ACM] hdu 1251 统计难题 (字典树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
- HDU 1251 统计难题 (字符串-Trie树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- C++常用排序法、随机数
C++常用排序法研究 2008-12-25 14:38 首先介绍一个计算时间差的函数,它在<time.h>头文件中定义,于是我们只需这样定义2个变量,再相减就可以计算时间差了. 函数开头加 ...
- Minimum Window Substring leetcode java
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- 【Gson】简介 文档 基本使用 示例
简介 new TypeToken<List<Person>>() {}.getType() 1 1 1 new TypeToken<List<Person> ...
- Java中浮点类型的精度问题 double float
要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法与浮点数的结构组成.因为机器只认识01,你想表示小数,你要机器认识小数点这个东西,必须采用某种方法.比如,简单点的,float四个字 ...
- 记录C#错误日志工具
在编程过程中,我们经常会用try...catch处理可能出错的代码块.如果程序出现错误,则直接show出错误信息. 当然,大型的系统都有错误日志处理模块,用数据库记录错误日志信息,有相应的写入错误日志 ...
- 深入理解JSON
一.JS判断字符串是否为JSON的方法: function isJSON(str) { if (typeof str == 'string') { try { JSON.parse(str); ret ...
- IOS Xib使用——为控制器添加Xib文件
Xib文件是一个轻量级的用来描述局部界面的文件,它与StoryBoard类似,都是使用Interface Bulider工具进行编辑.但是StoryBoard是重量级的,它是用来描述整个软件的多个界面 ...
- 交叉编译git
git依赖openssl.zlib. 首先编译openssl ./Configure linux-armv4 shared 修改Makefile,CC.RANLIB.MAKEDEPPROG为对应的交叉 ...
- Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
在文章开始,请你了解和熟悉openfire方面的相关知识,这样对你理解下面代码以及下面代码的用途有很好的了解.同时,你可能需要安装一个简单的CS聊天工具,来测试你的代码是否成功的在openfire服务 ...
- 解决Windows7 Embedded连接手机问题
故障现象:正确安装厂商自带的驱动后,插入安卓或iPhone手机,提示找到新硬件,却无法成功安装驱动.在此可以肯定的是:手机驱动无问题,手机.连接线也无问题.看来问题又落到“Embedded”上了! 仔 ...