这章刷的真带劲 嘿嘿

裸题

#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. C - Unary(map)

    Problem description Unary is a minimalistic Brainfuck dialect in which programs are written using on ...

  2. 如何正确产看API

    看API时,先看的它的父接口自接口,及其相关的抽象类和子类 看完后,看概述的第一段话就行,后面的不用看. 再看构造方法,并到底层去看构造方法里参数的具体含义. 最后,再将包含的方法一个个进行测试. 解 ...

  3. 单元测试之Mock

    为什么需要Mock. 真实对象具有不确定的行为.所以会产生不可预测的结果. 真实对象很难被创建. 真实对象的某些行为很难被触发(如网络错误). 真实对象令程序的运行速度很慢. 真实对象有(或者是)用户 ...

  4. python3 常用模块详解

    这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...

  5. 微信小程序开发之animation动画实现

    1. 创建动画实例 wx.createAnimation(OBJECT) 创建一个动画实例animation.调用实例的方法来描述动画.最后通过动画实例的export方法导出动画数据传递给组件的ani ...

  6. 【Python3】POP3协议收邮件

    初学Python3,做一个email的例子,虽然知道做的很渣渣,还是分享一下吧 POP3协议 POP3全称Post Official Protocol3,即邮局协议的第三个版本,它规定了怎样将个人计算 ...

  7. IAAS: IT公司去IOE-Alibaba系统构架解读

    从Hadoop到自主研发,技术解读阿里去IOE后的系统架构 原地址:...................... 云计算阿里飞天 摘要:从IOE时代,到Hadoop与飞天并行,再到飞天单集群5000节 ...

  8. VTK+MFC 系列教程 非常强大

    虽然QT才是王道!MFC的懂一些也是好的. 原文链接:http://blog.csdn.net/www_doling_net/article/details/8939115 之前介绍了基于VTK的单文 ...

  9. springmvc中freemarker的搭建

    在springmvc框架已经搭好的前提下,导入freemarker.jar,此处版本是2.3. 主要的配置工作都在spring-servlet.xml中,请看代码: <!--配置试图解析器 -- ...

  10. Swfit4.0中JSON与模型原生互转(JSONEncoder/JSONDecoder的使用)

    在Objective-C中,苹果并没有提供JSON转模型(模型转JSON)的接口,往往在开中需要添加第三库来处理JSON数据,比如:JsonModel.MJExtension.Mantle.JsonK ...