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字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- leetcode 几道题目
是周六晚上的几道题,晚上11点半,睡的早,起不来! 494. Target Sum 分析:看完这题,看到数据范围,长度20,枚举就是1<<20 = 1e6, 然后单次20,总共就是2e8, ...
- Hadoop MapReduce编程 API入门系列之网页排序(二十八)
不多说,直接上代码. Map output bytes=247 Map output materialized bytes=275 Input split bytes=139 Combine inpu ...
- Tomcatsession共享方案--memcached-session-manager
https://github.com/magro/memcached-session-manager/wiki/SerializationStrategies MSM的特性: a.支持t ...
- 创建一个netcore2.0和angular的项目并运行起来
netcore2.0发布了,喜大普奔. 我们先下载SDK,请看张善友老师的这篇博客 http://www.cnblogs.com/shanyou/p/7363037.html 下载完之后 我用的vs2 ...
- lua闭包函数
function createCountdownTimer(second) local ms = second * local function countDown() ms = ms - retur ...
- javascript实现选项卡切换的4种方法
方法一:for循环+if判断当前点击与自定义数组是否匹配 <html lang="en"> <head> <meta charset="UT ...
- classname在JavaScript中的应用
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- SQL Server-语句类别、数据库范式、系统数据库组成
前言 终于等到这一天,我要开始重新系统学习数据库了,关于数据库这块,不出意外的话,每天会定时更新一篇且内容不会包含太多,简短的内容,深入的理解,Always to review the basic ...
- MVC 接收文件
[HttpPost] public ActionResult Layedit() { var files = Request.Files; //获得所上传的所有文件 ) { HttpPostedFil ...
- mysql_connect() 不支持 请检查 mysql 模块是否正确加载
php的扩展 没有配置好! 打开php.ini文件: 搜索pdo_mysql和curl ;extension=php_curl.dll ;extension=pdo_mysql.dll 然后把2者前面 ...