HDOJ1251(字典树)

Xredman posted @ Mar 30, 2010 08:41:59 AM in Algorithm with tags HDU 字典树 , 1199 阅读

就是字典树

 

//2267164 2010-03-30 13:19:45 Accepted 1251 93MS 43800K 1607 B C++ 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <malloc.h>
using namespace std;

const int N = 26;

typedef struct dicTNode
{
	struct dicTNode *child[N];
	int cnt;
}dicTNode, *dictree;

void init(dictree &root)
{//对根节点初始化,根节点不存储任何数据
	root = new dicTNode;
	for(int i = 0; i < N; i++)
		root->child[i] = NULL;
	root->cnt = 0; 
}

void insert(char *str, dictree &root)
{
	dictree curNode, newNode;
	int len = strlen(str);
	int i, j;
	
	if(len == 0)
		return;//无须插入的情况
	curNode = root;
	for(i = 0; i < len; i++)
	if(curNode->child[str[i] - 'a'])
	{//子树存在
		curNode = curNode->child[str[i] - 'a'];
		curNode->cnt++; 
	} else
	{//子树不存在的情况 
		newNode = new dicTNode;
		for(j = 0; j < N; j++)
			newNode->child[j] = NULL;
		newNode->cnt = 1;
		
		curNode->child[str[i] - 'a'] = newNode;
		curNode = curNode->child[str[i] - 'a']; 
	}
}

int find(char *str, dictree root)
{
	int i, len;
	dictree curNode;
	len = strlen(str);
	
	if(len == 0)
		return 0;
	curNode = root;
	for(i = 0; i < len; i++)
	{//一直遍历到串底
		if(curNode->child[str[i] - 'a'])
			curNode = curNode->child[str[i] - 'a'];
		else
			return 0; 
	}
	return curNode->cnt;
}

void dfs(dictree &tc)
{
	if(tc)
	{
		for(int i = 0;i < N; i++)
			dfs(tc->child[i]);
	}
	free(tc);
	tc = NULL;
}
int main()
{
	int n, j, ll;
	char str[12];
	dictree root;
	init(root);
	while(gets(str), strcmp(str, "") != 0)
		insert(str, root);
	while(scanf("%s", str) != EOF)
	{
		printf("%d\n", find(str, root));
	}
	return 0;
}






destinycard.com 说:
Sep 03, 2023 10:53:30 PM

The Destiny Card is one of the better cards for people with poor or no credit to show to banks that they are eligible. As a result, a growing number of people are taking advantage of the destiny card and destinycard.com searching for ways to activate Destiny Card at destinycard.com/activate. Destiny’s card is a credit card, similar to a postpaid card. For example, credit cards provide unsecured loans for transactions that may not be debited immediately. Destiny Card gives you 45 days time to repay the money before it is debited from your bank account.

jnanabhumiap.in 说:
Jan 03, 2024 12:33:04 AM

JNANABHUMI AP provides a CBSE syllabus for all classes for the academic year 2024 has been designed as per the guidelines of the CBSE Board. The syllabus offers a conceptual background and lays the groundwork for the Class 10 Board exams. jnanabhumiap.in By visiting the page, students will find the downloadable pdf of the reduced CBSE 10th Syllabus along with the deleted portion of the syllabus for each subject. So, students are advised to prepare for the exam, as per the syllabus mentioned here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter