题目链接: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. Python-OpenCV中的图像轮廓检测

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

  2. explain SQL语句()

    坊间有传言:MySQL性能优化有个神器,叫做explain,它可以对select语句进行分析并且输出详细的select执行过程的详细信息,让开发者从这些信息中获得优化的思路. 下面来讲讲这个MySQL ...

  3. springmvc ajax 简单例子

    1.控制器曾 @Controller public class AjaxController { @RequestMapping("/ajax") public void ajax ...

  4. Java: 面向对象程序设计(上)

    1. 类 类主要包含3个部分: 类属性:用来描述类本身所抽象出的事物的属性 类方法:用来描述这个被抽象出的事物可以做什么 构造方法:每个类都至少会有一个特殊的方法,该方法提供了创建类对象的初始化机制 ...

  5. 3、kubernetes应用快速入门190625

    一.kubernetes应用入门 1.kubectl命令 Basic Commands create Create a resource from a file or from stdin. expo ...

  6. ubuntu下ganglia3.7.2编译安装

    一.介绍 ganglia主要包括gmond和gmeta 1.gmond用于收集监测数据,可以发送也可以接收在同一个组播或单播通道上的统计信息.gmond有两个角色,一个是发送者,另一个是接收者.当mu ...

  7. Ubuntu系统下同时打开多个终端窗口的软件

    sudo apt-get install Terminator

  8. VMware安装CentOS7的详细过程

    原文:https://www.jianshu.com/p/ce08cdbc4ddb?utm_source=tuicool&utm_medium=referral 本篇文章主要介绍了VMware ...

  9. spring技术小结

    1.DI和IOC 依赖注入(Dependency Injection)还是控制反转(Inversion of Conctrol) bean通过依赖注入,注册到spring容器里面.spring容器通过 ...

  10. <Openssl下hash函数>

    hash函数:是不可逆的函数,它的输入可以是任意长度的字节流.它的输出是固定大小的,hash函数的作用就是给你的文件产生一个摘要,它是独一无二的. 例如:y=f(x) x代表输入  y代表输出   输 ...