HDU1251-统计难题

统计难题

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


Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 

Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.
 

Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 

Sample Input
banana band bee absolute acm ba b band abc
 

Sample Output
2 3 1 0
 
解题思路:
刚开始采用的方法是字典树+DFS,然后超了内存,是因为结构体里面每一个都多开了一个长度为26的数组,用来保存,当前结点下接的其他结点的编号值,方便统计。
然后重新思考,因为每个单词都不会重复,所以每次进来一个单词,把以当前字符串前缀的数值直接+1保存就可以了。最后查找的时候直接找到最后一个字符保存的数值输出就可以了。
最后提交的时候注意用C++提交,其他提交时过不了的。

源代码:



优化版:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>

const int MAXN = 26;

typedef struct Trie
{
	int number;
	Trie* next[MAXN];
}Trie;
Trie *root;
int cnt=0;

void init()
{
	root = (Trie*)malloc(sizeof(Trie));
	root->number = 0;
	memset(root->next,NULL,sizeof(root->next));
}

void CreateTrie(char str[])
{
	Trie *q = root, *temp;
	int len = strlen(str);
	for (int i = 0; i<len; i++)
	{
		int id = str[i] - 'a';
		if (q->next[id] == NULL)
		{
		    //证明新单词有新的字符,当前的number值设置为1
			temp = (Trie*)malloc(sizeof(Trie));
			temp->number = 1;
			memset(temp->next,NULL,sizeof(temp->next));
			q->next[id] = temp;
			q = q->next[id];
		}
		else
		{
		    //虽然是新单词但是目前为止没有出现新字符,在原有的基础上直接加1
			q->next[id]->number++;
			q = q->next[id];
		}
	}
}

void FindTrie(char str[])
{
	Trie *q = root;
	cnt = 0;
	int len = strlen(str);
	for (int i = 0; i<len; i++)
	{
		int id = str[i] - 'a';
		if (q->next[id] != NULL)
		{
			q = q->next[id];
		}
		else
		{
			cnt = 0;
			return;
		}
	}
	cnt = q->number;
}

void getData()
{
	char s[15];
	while (gets(s))
	{
		if (!strcmp(s,""))
		{
			break;
		}
		CreateTrie(s);
	}
}

void slove()
{
	char s[15];
	while (~scanf("%s",s))
	{
		FindTrie(s);
		printf("%d
", cnt);
	}
	delete(root);
}

int main()
{
	init();
	getData();
	slove();
	return 0;
}

未优化版:



#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<malloc.h>
using namespace std;

const int MAXN = 26;

struct Trie
{
    int index;
    int ans[MAXN];
    Trie* next[MAXN];
};
Trie *root;
int cnt=0;

void init()
{
    root = (Trie*)malloc(sizeof(Trie));
    root->index = 0;
    for (int i = 0; i<MAXN; i++)
    {
        root->next[i] = NULL;
    }
    memset(root->ans, 0, sizeof(root->ans));
}

void CreateTrie(string str)
{
    Trie *q = root, *temp;
    int len = str.length();
    for (int i = 0; i<len; i++)
    {
        int id = str[i] - 'a';
        if (q->next[id] == NULL)
        {
            temp = (Trie*)malloc(sizeof(Trie));
            temp->index = 0;
            for (int j = 0; j<MAXN; j++)
            {
                temp->next[j] = NULL;
            }
            memset(temp->ans, 0, sizeof(temp->ans));
            q->next[id] = temp;
            q->ans[q->index] = id;
            q->index++;
            q = q->next[id];
        }
        else
        {
            q = q->next[id];
        }
    }
}

void DFS(Trie*q)
{
    Trie *temp=q;
    if (q->index == 0)
    {
        cnt++;
        return;
    }
    for (int i = 0; i<q->index; i++)
    {
        temp = q->next[q->ans[i]];
        DFS(temp);
    }
}

void FindTrie(string str)
{
    Trie *q = root;
    cnt = 0;
    int len = str.length();
    for (int i = 0; i<len; i++)
    {
        int id = str[i] - 'a';
        if (q->next[id] != NULL)
        {
            q = q->next[id];
        }
        else
        {
            cnt = 0;
            return;
        }
    }
    DFS(q);
}

void getData()
{
    string s;
    while (getline(cin, s))
    {
        if (s == "")
        {
            break;
        }
        CreateTrie(s);
    }
}

void slove()
{
    string s;
    while (cin >> s)
    {
        FindTrie(s);
        printf("%d
", cnt);
    }
    delete(root);
}

int main()
{
    init();
    getData();
    slove();
    return 0;
}


原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776050.html