这章刷的真带劲 嘿嘿

裸题

#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的更多相关文章

  1. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  2. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

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

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

  4. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  7. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

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

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

随机推荐

  1. BZOJ 2794 DP

    思路: 考虑把询问离线 按照m排序 物品按照a排序 f[i]表示c[j]的和到i b的最大值 背包就好 O(nk)竟然能过-- //By SiriusRen #include <cstdio&g ...

  2. B - Guess a number!

    Problem description A TV show called "Guess a number!" is gathering popularity. The whole ...

  3. poj3083 Children of the Candy Corn 深搜+广搜

    这道题有深搜和广搜.深搜还有要求,靠左或靠右.下面以靠左为例,可以把简单分为上北,下南,左西,右东四个方向.向东就是横坐标i不变,纵坐标j加1(i与j其实就是下标).其他方向也可以这样确定.通过上一步 ...

  4. MFC常用控件之滚动条

    近期学习了鸡啄米大神的博客,对其中的一些知识点做了一些自己的总结.不过,博客内容大部分来自鸡啄米.因此,这个博客算是转载博客,只是加了一些我自己的理解而已.若想学习鸡啄米大神的博客总结,请点击连接:h ...

  5. 重载(overload)和重写(override)的对比(笔试经常出)

    Day04_SHJavaTraing_4-6-2017 1.重载(overload):    ①权限修饰符(public private 默认):        无关    ②返回值类型:       ...

  6. 三维重建:深度相机方案对比-KinectFusion的基本原理(尺度)

    算法原理请参考此文:  kinect fusion 3D重建基本算法  http://log.csdn.net/xiaohu50/article/details/51592503 三维重建为三维空间实 ...

  7. CSS读书笔记(2)---简易相册和日历表的制作

    一.HTML和CSS制作的简易相册 相册在默认情况下是缩略图显示,而且是截取相片的某一部分显示的.当鼠标停留在某张缩略图上,相册列表中的缩略图变为大图,展示在相册的左边区域, 同时缩略图部分变成空的 ...

  8. JDBCUtils 工具类

    import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource;import java.io.IOE ...

  9. Java中的自动转换

    特点: 1. 系统自动完成的,不需要程序员手动修改代码 2.将 取值范围小的类型 自动提升为 取值范围大的类型 注意: 整数类型直接写会默认为int  小数类型直接写默认为double 类型的范围大小 ...

  10. mysql出错ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)

    其他的贴会教你 1.键盘上win+r 2.输入cmd 3.输入net  start mysql 但是还是没用 你可以试试 1.右击开始菜单 2.点击windows PowerShell(i) 3.输入 ...