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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- 玩转kafka
http://zookeeper.apache.org/releases.html#download http://kafka.apache.org/downloads.html(下载最新 二进制版本 ...
- OTL翻译(10) -- OTL的流缓冲池
OTL的流缓冲池 一般来讲,流一般作为一个局部的变量被使用,当使用完毕后就立刻关闭,如果需要再次使用就需要再次的声明变量,如此循环.OTL流的缓冲池(内存池)是一个解决以往的流性能低下的一个机制.当流 ...
- ubuntu/wireshark: There are no interfaces on which a capture can be done.故障解决
[转载]http://blog.csdn.net/ccwwff/article/details/6697258 在ubuntu安装wireshark, 在启动程序启动wireshark. 点captr ...
- 判断 iframe 是否加载完毕
我能想到的有以下几种方式: 方法一.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(function( ...
- Win10系统下面的TR1008解决方案
最近为了体验高大上的Win10系统,于是就把本本原来的Win7旗舰版 升级 到了Win10专业版.之后又在本本上部署了cognos,但是在打开Transform导入IQD数据源的时候就报错了,错误和之 ...
- 移动立方体算法(Marching cubes algorithm)
百度百科: 医学图像三维重建的方法主要有两大类:一类是三维面绘制,另一类是三维体绘制.体绘制能够更真实地反映物体结构,但由于其运算量大,即使利用高性能的计算机也无法满足实际应用中交互操作的需要.因此, ...
- MySQL 的实时性能监控利器
操作系统及MySQL数据库的实时性能状态数据尤为重要,特别是在有性能抖动的时候,这些实时的性能数据可以快速帮助你定位系统或MySQL数据库的性能瓶颈,就像你在Linux系统上使用「top,sar,io ...
- 从Ubuntu12.04LTS到Foreda19再到Foreda8
装Ubuntu的初衷是以为它能识别我的PCI无线网卡,但装了两遍没有做到. 昨天在Ubuntu装jdk7,其过程与正常Linux安装jdk差别不小,有点背离的意思.另外VI的用法也和正常Unix/Li ...
- mysql命令的一些参数
一mysql命令行参数 Usage: mysql [OPTIONS] [database] //命令方式 -?, --help //显示帮助信息并退出 -I, --help //显示帮助信息并退出 - ...
- IOS开发基础Object-C(12)—单例模式
单例模式的意思就是仅仅有一个实例. 单例模式确保某一个类仅仅有一个实例,并且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类仅仅能有一 ...