UVALive 7043 International Collegiate Routing Contest(字典树)

思路:读懂题目....这很关键....


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=3*1e4+10;
const int mask=(1<<8)-1;
int T,cas=1;
int n,ansn;
unsigned int a[N],len[N],ans[N<<1],al[N<<1];

const int maxc=2;
int ch[N<<5][maxc],si[N<<5],size;
void init()
{
	memset(ch[1],0,sizeof(ch[1]));
	si[1]=0;
	size=2;
	ansn=0;
}
void insert(int s,int BIT)
{
	if(BIT==0)
	{
		si[1]=1;
		return;
	}
	int u=1;
	for(int i=31;i>31-BIT;i--)
	{
		int c=(s&(1<<i)?1:0);
		if(!ch[u][c])
		{
			memset(ch[size],0,sizeof(ch[0]));
			si[size]=0;
			ch[u][c]=size++;
		}
		u=ch[u][c];
	}
	si[u]=1;
}
void dfs(int u,unsigned int sum,int depth)
{
	if(si[u]) return;
	if(!ch[u][0]&&!ch[u][1])
	{
		ans[ansn]=sum;
		al[ansn++]=depth;
		return;
	}
	if(ch[u][0]) dfs(ch[u][0],sum,depth+1);
	else
	{
		ans[ansn]=sum;
		al[ansn++]=depth+1;
	}
	if(ch[u][1]) dfs(ch[u][1],sum|(1U<<(31-depth)),depth+1);
	else
	{
		ans[ansn]=sum|(1U<<(31-depth));
		al[ansn++]=depth+1;
	}
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			a[i]=0;
			for(int j=0,x;j<4;j++)
			{
				scanf("%d%*c",&x);
				//printf("%d ",x);
				a[i]=a[i]<<8|x;
			}
			scanf("%d",len+i);
			//printf("%d\n",len[i]);
			insert(a[i],len[i]);
		}
		dfs(1,0,0);
		printf("Case #%d:\n",cas++);
		printf("%d\n",ansn);
		for(int i=0;i<ansn;i++)
		{
			int b[4];
			for(int j=3;j>=0;j--) 
			{
				b[j]=ans[i]&mask;
				ans[i]>>=8;
			}
			printf("%d.%d.%d.%d/%d\n",b[0],b[1],b[2],b[3],al[i]);
		}
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值