hdu_1251 统计难题

news/2024/7/8 15:36:51 标签: null, output, build, 测试

统计难题

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251

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

 

解题思路:

         建立字典树,将输入的数据插入到字典树中,每个字典树定义一个times值,记录没过字符出现的次数,在查找的时候判断如果不存在该字符,输出0,如果存在,则输出该字符所在的times值,这题我觉得比较考验的是输入,不好输入,如果用scanf(“%s”,ch);输入字符串的话不能接收回车,所以只能用字符输入

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>

#define maxn 27
using namespace std;

struct Trie
{
	Trie *next[maxn];
	int times;
};


void build(Trie *root,char *str,int len)
{
	int i,j;
	Trie *p=root;
	Trie *q;
	//int len=strlen(str);
	for(i=0;i<len;i++)
	{
		int id=str[i]-'a';
		if(p->next[id]!=NULL)
		{
			p=p->next[id];
			p->times++;
			continue;
		}
		q=(Trie *)malloc(sizeof(Trie));
		q->times=1;
		for(j=0;j<maxn;j++)
		{
			q->next[j]=NULL;
		}
		p->next[id]=q;
		p=p->next[id];
	}

}

int findTrie(Trie *root,char *str)
{//查询字符串出现的次数
	int len=strlen(str);
	Trie *p=root;
	int times=0;
	for(int i=0;i<len;i++)
	{
		int id=str[i]-'a';
		if(p->next[id]==NULL)
		{
			times=0;
			return times;
		}
		p=p->next[id];
		if(i==len-1)
		{
			times=p->times;
			return times;
		}
		
	}
	return 0;

}
int main()
{
	Trie *root;
	int j;
	root=(Trie *)malloc(sizeof(Trie));
	for(j=0;j<maxn;j++)
	{
		root->next[j]=NULL;
	}	
	while(true)
	{
		char ch[10];
		int i=0;
		while(true)
		{
			ch[i]=getchar();
			if(ch[i]=='\n')break;		
			i++;
		}
		if(i==0)
			break;
		build(root,ch,i);
	}
	char ch[10];
	while(scanf("%s",&ch)!=EOF)
	{
		printf("%d\n",findTrie(root,ch));
	}
	return 0;
}


 


http://www.niftyadmin.cn/n/862675.html

相关文章

看看DBA应该具备怎样的素质

近年来&#xff0c;我一直在和数据库管理员打交道&#xff0c;并直接面试了很多DBA职位。本文想概括一下IT行业对DBA的要求&#xff0c;以及国内DBA的新资现状。可以肯定地说&#xff0c;做一个高级DBA是很不错的职业。如果你打算成为一名DBA&#xff0c;那么希望本文起到抛砖引…

poj_1129 Channel Allocation

Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9669 Accepted: 4919 题目链接&#xff1a;http://poj.org/problem?id1129 Description When a radio station is broadcasting over a very largearea, repeaters are used to retransm…

2021.04.27【R语言】丨箱线图无法显示解决办法

摘要 箱线图主要用于反映原始数据分布的特征&#xff0c;还可以进行多组数据分布特征的比 较。箱线图的绘制方法是&#xff1a;先找出一组数据的上边缘、下边缘、中位数和两个四分位数&#xff1b;然后&#xff0c; 连接两个四分位数画出箱体&#xff1b;再将上边缘和下边缘与箱…

C++实现Adapter模式

由于CSDN长时间无法显示图片&#xff0c;本文已暂时迁移到&#xff1a; http://patmusing.blog.163.com/blog/static/13583496020100231823927/

poj_2387 Til the Cows Come Home

Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20550 Accepted: 6864 题目链接&#xff1a;http://poj.org/problem?id2387 Description Bessie is out in the field andwants to get back to the barn to get as much sleep as …

C++实现Bridge模式

由于CSDN长时间无法显示图片&#xff0c;本文已暂时迁移到&#xff1a; http://patmusing.blog.163.com/blog/static/13583496020100231174397/ 后续设计模式的C实现&#xff0c;会在玄机逸士的网易博客中陆续给出&#xff0c;欢迎大家前去指正。

2021.04.28丨VIM/VI跳转行常用操作

vim/vi操作 1.跳到文本的最后一行&#xff1a;按“G”,即“shiftg” 2.跳到最后一行的最后一个字符 &#xff1a; 先重复1的操作即按“G”&#xff0c;之后按“$”键&#xff0c;即“shift4”。 3.跳到第一行的第一个字符&#xff1a;先按两次“g”&#xff0c; 4.跳转到当前行…

poj_3984 迷宫问题

迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4812 Accepted: 2737 题目链接&#xff1a;http://poj.org/problem?id3984 Description 定义一个二维数组&#xff1a; int maze[5][5] { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0…