0x16 Trie
这章刷的真带劲 嘿嘿
裸题
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct Trie
{
int c,w[];
Trie(){c=;memset(w,,sizeof(w));}
}tr[];int trlen;
char ss[];
void maketree()
{
int now=,len=strlen(ss+);
for(int i=;i<=len;i++)
{
int x=ss[i]-'a'+;
if(tr[now].w[x]==)
tr[now].w[x]=++trlen;
now=tr[now].w[x];
}
tr[now].c++;
}
int getsum()
{
int now=,sum=,len=strlen(ss+);
for(int i=;i<=len;i++)
{
int x=ss[i]-'a'+;
if(tr[now].w[x]==)return sum;
now=tr[now].w[x];
sum+=tr[now].c;
}
return sum;
}
int main()
{
int n,Q;
scanf("%d%d",&n,&Q);
trlen=;
for(int i=;i<=n;i++)
scanf("%s",ss+), maketree(); while(Q--)
{
scanf("%s",ss+);
printf("%d\n",getsum());
}
return ;
}
前缀统计
字典树算最大异或和的老套路
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct Trie
{
int c,w[];
Trie(){c=;memset(w,,sizeof(w));}
}tr[];int trlen;
int D; int mmax;
void ask()
{
int now=,sum=;
for(int i=;i<=;i++)
{
int x= ( D&(<<(-i)) ) ? :;
if(tr[now].w[x]!=)
{
sum+=(<<(-i));
now=tr[now].w[x];
}
else now=tr[now].w[x^];
}
mmax=max(mmax,sum);
}
void insert()
{
int now=;
for(int i=;i<=;i++)
{
int x= ( D&(<<(-i)) ) ? :;
if(tr[now].w[x]==)
tr[now].w[x]=++trlen;
now=tr[now].w[x];
}
} int main()
{
int n;mmax=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&D);
if(i!=)ask();
insert();
}
printf("%d\n",mmax);
return ;
}
The XOR Largest Pair
同上,变形一下就行了,注意字典树的清空啊!!
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct Trie
{
int w[];
}tr[];int trlen;
int D,mmax,c[];
void ask()
{
int now=,sum=;
for(int i=;i<=;i++)
{
int x= ( D&(<<(-i)) ) ? :;
if(tr[now].w[x]!=)
{
sum+=(<<(-i));
now=tr[now].w[x];
}
else now=tr[now].w[x^];
}
mmax=max(mmax,sum);
}
void insert()
{
int now=;
for(int i=;i<=;i++)
{
int x= ( D&(<<(-i)) ) ? :;
if(tr[now].w[x]==)
tr[now].w[x]=++trlen, tr[trlen].w[]=tr[trlen].w[]=;
now=tr[now].w[x];
}
} struct node
{
int x,y,d,next;
}a[];int len,last[];
void ins(int x,int y,int d)
{
len++;
a[len].x=x;a[len].y=y;a[len].d=d;
a[len].next=last[x];last[x]=len;
}
void dfs(int x,int F)
{
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y!=F)
{
c[y]=c[x]^a[k].d;
dfs(y,x);
}
}
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
len=;memset(last,,sizeof(last));
for(int i=;i<n;i++)
{
int x,y,d;
scanf("%d%d%d",&x,&y,&d);x++,y++;
ins(x,y,d);ins(y,x,d);
}
c[]=;dfs(,); mmax=;trlen=;tr[].w[]=tr[].w[]=;
for(int i=;i<=n;i++)
{
D=c[i];
if(i!=)ask();
insert();
}
printf("%d\n",mmax);
}
return ;
}
poj3764
0x16 Trie的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- 「Canvas」玩转
作者: 糖少 简介 Canvas是 HTML5 新增的,一个可以使用脚本(通常为JavaScript)在其中绘制图像的 HTML 元素.它可以用来制作照片集或者制作简单(也不是那么简单)的动画,甚至可 ...
- Pop3协议详解
POP3全称为Post Office Protocol version3,即邮局协议第3版.它被用户代理用来邮件服务器取得邮件.POP3采用的也是C/S通信 模型 用户从邮件服务器上接收邮件的典型 ...
- 批量插入 SqlBulkCopy的测试
关于SqlBulkCopy的测试 最近要做.net关于sql大量插入,找到了sqlbulkcopy(自己google下,应该很多说明了)这个好东西,于是测试下性能,用了三个方法对比: 1)直接用ado ...
- 百度地图api的简单应用
百度地图api 获取经纬度(通过浏览器的) //获取经纬度 window.navigator.geolocation.getCurrentPosition(function(position) { a ...
- JS和PHP之间以JSON格式传输
Json是一种的轻量级文本数据交换格式.它独立于编程语言,可以用于在不用的编程语言之间进行数据的交互. 下面简单例举二个使用JSON进行数据通信的例子. 第一个例子: //Javascript以aja ...
- 有关Gradle Network is unreachable: connect的报错
项目Gradle Errer:Network is unreachable: connect 同时还有as的 报错 Internal HTTP server disabled: Cannot st ...
- [ Java ] String 轉型 ArrayList
Lambda 對我而言一很像天書 這個行 Java code 讓我開始有點些微有 Lambda 感覺 https://stackoverflow.com/questions/10706721/conv ...
- Py基础+中级
原文 Py学习博客 1:https://www.cnblogs.com/fu-yong/p/8060198.html2:while True:just do it 三.Python的默认编码 ▷pyt ...
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'
ALTER TABLE table1 ADD COLUMN column1 VARCHAR(400) DEFAULT NULL; ERROR 1062 (23000): Duplicate entry ...
- ssh 免密码登入远程服务器
生成ssh密钥,将公钥上传至远程服务器~/.ssh目录下面(没有的话就建一个): ssh-keygen -t rsa scp ~/.ssh/id_rsa.pub root@yourserver.com ...