题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269 ,BestCoder Round #44的B题,关于字典树的应用。

  比赛的时候没想出做法,现在补上。


题解:

  我们考虑,当lowbit(A xor B) = 2p时,A和B表示的二进制数的后p-1位肯定相同。于是我们可以维护一棵字典树,对于每个数x,可以将其转换为30位的二进制数(不足30位的在前面补0),将该二进制数逆序后插入字典树。统计答案时,对于Ai我们先将其同上述做法转换为30位的二进制数,然后逆序后在字典树中查找,对于路径上的每个结点x,如果它下一步对应的边是v,则和它xor后lowbit为2k的数有cnt(x , !v)个。cnt(x , v)表示x的对应的v这条边的子树个数。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn = + ;
const int MOD = ;
const int sigma_size = ;
struct Trie {
int ch[maxn * ][sigma_size];
LL cnt[maxn * ] , pow[];
int size;
void init() {
size = pow[] = ;
memset(ch[] , , sizeof(ch[]));
memset(cnt , , sizeof(cnt));
for(int i = ; i < ; i++)
pow[i] = pow[i - ] << ;
}
int index(char c) { return c - ''; }
void insert(char *s) {
int i , rt;
for(i = rt = ; s[i] != '\0' ; i++) {
int c= index(s[i]);
if(!ch[rt][c]) {
memset(ch[size] , , sizeof(ch[size]));
ch[rt][c] = size++;
}
rt = ch[rt][c];
cnt[rt] = (cnt[rt] + ) % MOD;
}
}
LL find(char *s) {
int i , rt;
LL ret;
for(i = rt = ret = ; s[i] != '\0' ; i++) {
int c = index(s[i]);
if(ch[rt][!c]) {
int tmp = ch[rt][!c];
ret = (ret + (pow[i] * (1LL * cnt[tmp]))) % MOD;
}
rt = ch[rt][c];
}
return ret;
}
} trie;
void binary(int x , char *s)
{
int i = ;
while(x) {
s[i++] = x % + '';
x >>= ;
}
while(i < ) s[i++] = '';
s[i] = '\0';
}
int a[maxn]; char s[]; int main()
{
int n , T;
cin >> T;
for(int cas = ; cas <= T ; cas++)
{
trie.init();
scanf("%d" , &n);
for(int i = ; i <= n ; i++) {
scanf("%d" , &a[i]);
binary(a[i] , s);
trie.insert(s);
}
LL ans = ;
for(int i = ; i <= n ; i++) {
binary(a[i] , s);
ans = (ans + trie.find(s)) % MOD;
}
printf("Case #%d: %I64d\n" , cas , ans);
}
return ;
}

HDU5269 字典树的更多相关文章

  1. HDU--5269 ZYB loves Xor I (字典树)

    题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制  我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...

  2. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  5. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  6. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  7. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  8. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  9. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 引用静态资源的url添加版本号,解决版本发布后的浏览器缓存有关问题

    在日常的工作中,我们经常会遇到页面文件(html,jsp等)中引用的js,css,图片等被修改后,而浏览器依然缓存着老版本的文件,客户一时半会看不到修改后的效果,同时也给生产环境的版本发布带来了一些问 ...

  2. OC官方文档翻译-Values-and-Collections-值与集合类型

    查看全部文档翻译,请浏览https://github.com/L1l1thLY/Programming-with-Objective-C-in-Chinese,blog仅收录本人翻译的两章. 简述 O ...

  3. Spring深入学习

    涨知识系列 Environment environment = context.getEnvironment(); 在Spring中所有被加载到spring中的配置文件都会出现在这个环境变量中, 其中 ...

  4. Redis源码分析-底层数据结构盘点

    前段时间翻看了Redis的源代码(C语言版本,Git地址:https://github.com/antirez/redis), 过了一遍Redis数据结构,包括SDS.ADList.dict.ints ...

  5. [WIP]JavaScript import, export

    创建: 2019/06/14 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import h ...

  6. Python-OpenCV中的图像轮廓检测

    目录 cv2.findContours()   主要记录Python-OpenCV中的cv2.findContours()方法:官方文档: cv2.findContours()   在二值图像中寻找图 ...

  7. scrapy框架爬取蜂鸟网的人像图片

    今天有点无聊,本来打算去蜂鸟网爬点图片存起来显得自己有点内涵,但是当我点开人像的时候就被里面的小姐姐所吸引了,下面就是整个爬图片的思路和过程了 第一步:先创建一个爬虫项目 scrapy startpr ...

  8. ldap第二天-yum安装LDAP + phpLDAPadmin

    1.安装LDAP服务器和客户端,migrationtools工具包 yum install -y openldap-servers openldap-clients migrationtools 2. ...

  9. Zabbix之CentOS7.3下yum安装Zabbix3.5

    Zabbix特点介绍 (此介绍来源于https://www.zabbix.com/documentation/3.4/zh/manual/introduction/features) 概述Zabbix ...

  10. .db文件打开方式

    有时在工作中,数据库格式db后缀的格式,直接是打不开的,所以我这里使用了数据库管理工具,步骤如下 1. 在电脑安装 Navicat Premium,安装后在桌面生成图标,点击图标打开程序. 2.打开程 ...