HDU1251 统计难题(经典前缀树入门题)

题目大意:给你一系列单词,然后再给你一系列查询,问你每个查询的字符串是哪几个单词的前缀,输出每个单词查询的前缀个数,单词长度不超过10

题目思路:这道题目可以用hash来做,但我是学习前缀树来着,看hash干哈(一边去
首先我们根据已有单词创建一颗前缀树(不知道前缀树是什么?百度吧骚年)
创建一个trie数据结构,作为前缀树,前缀树的模板

// 字典树(前缀树)
struct trie{
    
    
	int tree[1000008][26],cnt;
	int color[1000008];

	// 插入一个新单词
	void insert(char* s,int l){
    
    
		int p = 0;
		for(int i=0;i<l;++i){
    
    
			int c = s[i] - 'a';
			if(!tree[p][c])
				tree[p][c] = ++cnt;
			p = tree[p][c];
			++color[p];
		}
	}

	// 查找前缀访问的次数,也就是某个前缀在单词中出现的次数
	int search(char* s,int l){
    
    
		int p = 0;
		for(int i=0;i<l;++i){
    
    
			int c = s[i] - 'a';
			if(!tree[p][c]) return 0;
			p = tree[p][c];
		}
		return color[p];
	}

};

前缀树的查询时间复杂度是O(h),其中h是树的高度,由这道题目可以知道,平均单词数的高度,最长单词数就是树的最大高度,而本题最大单词数不超过10,所以查询时间复杂度是O(10) = O(1)
前缀树最大的耗时在于预处理插入,时间复杂度是O(n),n是单词个数
典型的空间换时间的数据结构

AC代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef __int64 bi;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){
    
    return x&(-x);}
ll gcd(ll a,ll b){
    
    return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){
    
    if(!b){
    
    d=a,x=1,y=0;}else{
    
    ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){
    
    ll res=1;a%=MOD;while(b>0){
    
    if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){
    
    return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){
    
    ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){
    
     ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {
    
    if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = {
    
     {
    
    1,0}, {
    
    -1,0},{
    
    0,1},{
    
    0,-1} };

// 字典树(前缀树)
struct trie{
    
    
	int tree[1000008][26],cnt;
	int color[1000008];

	// 插入一个新单词
	void insert(char* s,int l){
    
    
		int p = 0;
		for(int i=0;i<l;++i){
    
    
			int c = s[i] - 'a';
			if(!tree[p][c])
				tree[p][c] = ++cnt;
			p = tree[p][c];
			++color[p];
		}
	}

	// 查找前缀访问的次数,也就是某个前缀在单词中出现的次数
	int search(char* s,int l){
    
    
		int p = 0;
		for(int i=0;i<l;++i){
    
    
			int c = s[i] - 'a';
			if(!tree[p][c]) return 0;
			p = tree[p][c];
		}
		return color[p];
	}

};
struct trie tr;

signed main(void)
{
    
    
	// 输入单词,构建一颗前缀树
	char s[11];
	while(gets(s)!=NULL){
    
    
		if(s[0]=='\0') break;
		tr.insert(s,strlen(s));
	}
	// 输入查询,进行查询统计
	while(gets(s)!=NULL){
    
    
		cout << tr.search(s,strlen(s))<<endl;
	}
	
}

猜你喜欢

转载自blog.csdn.net/YSJ367635984/article/details/114124274