2014 西安站 I International Collegiate Routing Contest(trie)
来源:互联网 发布:数据抓取软件 编辑:程序博客网 时间:2024/06/01 17:52
题目链接:这里
题意:输入IPv4地址空间中的一些子网号,构成一个网络集合。
输出最小的一个网络集合,要求其与输入集合没有交集,且相对IPv4地址空间全集,是输入集合的补集。输出集合包含的子网号,格式遵循网络规范
做法:tire树,把所有输入的子网插入trie里面,然后遍历一下,如果一个结点的左儿子和右儿子有一个是标记过的,就输出没有标记的那个儿子的子网。
#include <bits/stdc++.h>using namespace std;#define pii pair<int, int>#define MP make_pair#define LL long long#define ls (i << 1)#define rs (ls | 1)#define md (ll + rr >> 1)#define lson ll, md, ls#define rson md + 1, rr, rs#define eps 1e-8#define inf 0x3f3f3f3f#define mod 1000000007#define N 100010#define M 200020#define fi first#define se secondchar buf[32000000],*pt = buf,*o = buf;int getint(){ int f = 1,x = 0; while((*pt != '-') && (*pt < '0' || *pt > '9')) pt ++; if(*pt == '-') f = -1,pt ++; else x = *pt++ - 48; while(*pt >= '0' && *pt <= '9') x = x * 10 + *pt ++ - 48; return x * f;}char getch(){ char ch; while(*pt < 'A' || *pt > 'Z') pt ++; ch=*pt;pt++; return ch;}int ch[N*30][2], cnt, tot, n;bool vis[N*30];pair<LL, int> p[N];int insert(LL v, int k){ int u = 1; for(int i = 31; i >= 0 && k; --i, --k){ int c = v >> i & 1; if(!ch[u][c]){ ch[u][c] = ++tot; memset(ch[tot], 0, sizeof ch[tot]); vis[tot] = 0; } u = ch[u][c]; } vis[u] = 1;}void print(LL v, int dep){ v <<= dep; int a = v >> 24; v ^= (LL)a << 24; int b = v >> 16; v ^= (LL)b << 16; int c = v >> 8; v ^= (LL)c << 8; int d = v; printf("%d.%d.%d.%d/%d\n", a, b, c, d, 32 - dep);}int query(int u, LL x, int dep){ if(u == 0 || vis[u]) return vis[u]; if(dep == -1) return vis[u]; int p1 = query(ch[u][0], x << 1, dep - 1); int p2 = query(ch[u][1], x << 1 | 1, dep - 1); if(p1 && !p2){ p[cnt++] = MP(x << 1 | 1, dep); } else if(!p1 && p2){ p[cnt++] = MP(x << 1, dep); } return p1 | p2;}int main(){ int cas, kk = 0; scanf("%d", &cas); while(cas--){ scanf("%d", &n); tot = 1; memset(ch[tot], 0, sizeof ch[tot]); memset(ch[0], 0, sizeof ch[0]); vis[0] = vis[1] = 0; for(int i = 0; i < n; ++i){ int a, b, c, d, k; scanf("%d.%d.%d.%d/%d", &a, &b, &c, &d, &k); LL v = 1LL * a * (1 << 24) + b * (1 << 16) + c * (1 << 8) + d; insert(v, k); } printf("Case #%d:\n", ++kk); if(n == 0){ printf("1\n0.0.0.0/0\n"); continue; } cnt = 0; query(1, 0LL, 31); printf("%d\n", cnt); for(int i = 0; i < cnt; ++i) print(p[i].fi, p[i].se); } return 0;}
0 0
- 2014 西安站 I International Collegiate Routing Contest(trie)
- 西安现场赛 Problem I. International Collegiate Routing Contest
- CF GYM 100548 International Collegiate Routing Contest(2014ACM西安现场赛Problem I)
- 2014西安现场赛I题 International Collegiate Routing Contest(字典树)
- CF GYM 100548 International Collegiate Routing Contest(2014ACM西安现场赛Problem I)不懂提
- Codeforces GYM 100548 I - International Collegiate Routing Contest 2014 ACM Xian Regional Contest
- 2014 ACM/ICPC Xi'an Regional I - International Collegiate Routing Contest
- Problem I. International Collegiate Routing ContestGym
- UVALive 7043 International Collegiate Routing Contest(字典树)
- UVALive 7043 International Collegiate Routing Contest 字典树
- UVALive 7043 International Collegiate Routing Contest(字典树)
- UVALive 7043 International Collegiate Routing Contest 字典树,递归
- The 31st ACM International Collegiate Programming Contest World Finals
- 2009 ACM International Collegiate Programming Contest Winners Announced
- 2017 ACM Jordanian Collegiate Programming Contest I(Counting Votes-特征值)
- Galactic Collegiate Programming Contest
- ACM International Collegiate Programming H.动态规划
- CF 2014 Nordic Collegiate Programming Contest E Opening Ceremony
- I/O多路复用之select模型
- JS入门之无缝滚动
- ADFS 账户同步到 SharePoint 使用
- 关于解决Android 6.0 下Apache下的Header类找不到报的配置错误
- SQL SERVER 2008 利用发布订阅方式实现数据库同步
- 2014 西安站 I International Collegiate Routing Contest(trie)
- DreamWeaver的站点设置
- 响应式编程、函数式编程、链式调用
- studio编译时错误提示:Plugin is too old, please update to a more recent,导致无法运行
- 免安装版mysql(ZIP解压缩版)
- 网易有道内推笔试题
- HTML中jquery控制button按钮可点击、不可点击
- springmvc form表单标签使用
- #415 Valid Palindrome