uva 11488 Hyper Prefix Sets

本文介绍了一种使用字典树实现的高效算法。通过插入和查询操作,该算法能够快速处理大量字符串数据,并计算出最优解。代码示例中详细展示了如何构建字典树结构、插入字符串以及进行查询的过程。

字典树水题。。。。。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;

#define rep(i,s,t) for(int i=s;i<t;i++)
#define N 500000

struct node{
	int next[2],num,len;
	void init(){
		memset(next,-1,sizeof(next));
		num=len=0;
	}
}root[N];

int tot,t,n,ans;
char s[210];

inline void insert(char *s){
	int p=0,id,l=strlen(s);
	rep(i,0,l){
		id=s[i]-'0';
		if(root[p].next[id]==-1){
			root[p].next[id]=tot;
			root[tot].init();
			root[tot++].len=root[p].len+1;
		}
		p=root[p].next[id];
		root[p].num++;
	}
}

inline void query(){
	queue<int>q;
	q.push(0);
	while(!q.empty()){
		int x=q.front();
		q.pop();
		ans=max(root[x].num*root[x].len,ans);
		rep(i,0,2){
			if(root[x].next[i]!=-1){
				q.push(root[x].next[i]);
			}
		}
	}
}

int main(){
	scanf("%d",&t);
	rep(ca,0,t){
		scanf("%d",&n);
		tot=0;
		root[tot++].init();
		rep(i,0,n){
			scanf("%s",s);insert(s);
		}
		ans=0;
		query();
		printf("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值