题目链接: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. SNAT端口转发配置

    需求说明 在只有外网地址的机器上也能正常访问内网地址 配置过程 环境网络信息 网络名称 网络地址 外网 192.168.200.0/24 (网关:192.168.200.251) 内网 92.0.0. ...

  2. JVM 零散知识

    年轻代大小选择: 响应时间优先的应用: 尽可能设大,直到接近系统的最低响应时间限制.在此种情况下,年轻代收集发生的频率也是最小的.同时,减少到达年老代的对象. 吞吐量优先的应用: 尽可能的设置大,可能 ...

  3. Openjudge jubeeeeeat

    jubeeeeeat 题目链接 总时间限制:  1000ms 内存限制:  256000kB 描述 众所周知,LZF很喜欢打一个叫Jubeat的游戏.这是个音乐游戏,游戏界面是4×4的方阵,会根据音乐 ...

  4. 洛谷P1054 等价表达式

    P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...

  5. [Xcode 实际操作]四、常用控件-(9)普通警告窗口的使用

    目录:[Swift]Xcode实际操作 本文将演示警告窗口的使用方法. 警告窗口不仅可以给用户展现提示信息,还可以提供若干选项供用户选择. 在项目导航区,打开视图控制器的代码文件[ViewContro ...

  6. MCP|DYM|Quantitative mass spectrometry to interrogate proteomic heterogeneity in metastatic lung adenocarcinoma and validate a novel somatic mutation CDK12-G879V (利用定量质谱探究转移性肺腺瘤的蛋白质组异质性及验证新体细胞突变)

    文献名:Quantitative mass spectrometry to interrogate proteomic heterogeneity in metastatic lung adenoca ...

  7. Maven中如何利用继承创建web项目(分层开发)

    1.创建父项目parent 新建——Maven Project——勾选Create a Simple Project(skip archetype selected)——填写parent坐标,注意打包 ...

  8. PAT天梯赛L3-005 垃圾箱分布

    题目链接:点击打开链接 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住.所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的 ...

  9. java文件操作文件之csv

    直接上代码: @Test public void dowrite(){ String filePath = "D://test.csv"; try { File f = new F ...

  10. angularJS处理table中checkbox的选中状态

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...