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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- [Git] git merge之squash
reference : https://www.cnblogs.com/ungshow/p/3515161.html 看CM源码时,发现历史记录里有很多squash,于是google了解了一下. Gi ...
- C++ 竞赛常用头文件
C.传统 C++ #include <assert.h> 设定插入点 #include <ctype.h> 字符处理 #include <errno.h> 定义错误 ...
- Objective-C:NSString类的常见用法
几种常见的用法为:字符串的创建.字符串的搜索.字符串的比较.字符串的转换 用途一:字符串的创建 void ex1() { //1.常量字符串的对象 NSString *str1 = @"he ...
- require.js 最佳实践
require.js是一个js库,相关的基础知识,前面转载了两篇博文:Javascript模块化编程(require.js), Javascript模块化工具require.js教程,RequireJ ...
- 适配 通知 Notification 通知渠道 前台服务 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Loadrunner视频教程汇总
小布老师视频:测试工具概述,兼LoadRunner介绍 -1-4http://www.boobooke.com/v/bbk1046http://www.boobooke.com/v/bbk1046.z ...
- Maven Dependencies没有了的解决办法
头疼的问题,maven Dependencies突然没有了,别的项目都有,个别的却怎么也出不来. 以下是某大神的解决方法,特此转发,以防丢失: 网上搜索了一番,大多都是下面这种做法: 右击 Mave ...
- hdu 5326
Work Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- [LeetCode] Distinct Subsequences [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- hiberante 二级缓存设置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...