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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- apache基金会项目及甲骨文项目汇总
Apache软件基金会 顶级项目 ▪ ActiveMQ ▪ Ant ▪ Apache HTTP Server ▪ APR ▪ Beehive ▪ Camel ▪ Cassandra ▪ Cayenne ...
- FFMPEG视音频编解码零基础学习方法 【荐】
在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频 编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在 ...
- Android触摸事件
简单介绍: 做了一个语音发送UI的小demo. 按下显示语音窗体,依据音量调节UI音量显示,上划至窗体显示取消发送. 原理: 1:获取什么事件来运行操作: 给Button加入setOnTouchLis ...
- iOS开发-沙盒(sandbox)机制
苹果前天发的财报,貌似现在用ios系统的比以前又多了一些,但是大家的iPhone购买的渠道也是五花八门,有的从非正规渠道购买的iPhone里的操作系统已经被越狱过,越狱这个事情和Android的roo ...
- 升级iOS10后http网页定位失效解决方案
最近我们在做项目时遇到这样一个新问题,用户在升级 iOS10 后,在 http 下使用 geolocation api 会报错,控制台输出 [blocked] Access to geolocatio ...
- 【HowTo ML】分类问题->神经网络入门
非线性分类器(Non-linear hypotheses) 为什么使用非线性分类器 我们举几个栗子: 假如我们有一个数据空间如左上角坐标系所看到的,那么我们要的模型须要如右边公式所看到的的预測函数. ...
- 【HBase】学习笔记
HBASE 1 简介 1.1 官网 1.1.1 http://hbase.apache.org/ 1.1.2 Apache HBase™ is the Hadoop database, a distr ...
- Oracle Agile PLM Web Services 的实现
Oracle 的产品Agile PLM内置了许多Web Services,其他系统可以通过Web Servcies实现对Agile PLM系统资源的访问.快速学会使用的方法,是去Oracle的官网下载 ...
- (转)AssetBundle系列——游戏资源打包(二)
转自:http://www.cnblogs.com/sifenkesi/p/3557290.html 本篇接着上一篇.上篇中说到的4步的代码分别如下所示: (1)将资源打包成assetbundle,并 ...
- core dump相关
linux下生成core dump文件方法及设置 http://www.2cto.com/os/201310/253450.html 在linux平台下,设置core dump文件生成的方法: 1 ...