这章刷的真带劲 嘿嘿

裸题

#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. js判读周末以及节假日

    两个链接,搞定! http://bbs.csdn.net/topics/10417668 http://www.jb51.net/article/131660.htm

  2. Oracle---显式游标

    一  游标的分类 在Oracle中提供了两种类型的游标:静态游标和动态游标. 1.静态游标是在编译时知道其SELECT语句的游标.静态游标又分为两种类型,即隐式游标和显式游标. 2.当用户需要为游标使 ...

  3. 4185 Oil Skimming 最大匹配 奇偶建图

    题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合 ...

  4. mac 下安装 mariadb

    通过brew 安装: brew install mariadb 初始化数据库 cd /usr/local/Cellar/mariadb/10.0.10/scripts mysql_install_db ...

  5. SpringMVC(五)@RequestHeader和@CookieValue

    通过使用@RequestHeader获取请求头 通过使用@CookieValue获取cookie值 代码: 1: @Controller 2: public class TestHeader_Cook ...

  6. 插入排序InsertSort

    插入排序:从第二个数开始  一直和前面的数组比较 获得排序定位 ​ 代码 /** *插入排序 */ public class InsertSort { public static void inser ...

  7. Chernobyl’ Eagle on a Roof(鹰蛋坚固度)

    链接 Chernobyl’ Eagle on a Roof 题意 引用论文题意:有一堆共 M 个鹰蛋,一位教授想研究这些鹰蛋的坚硬度 E.他是通过不断从一幢 N 层的楼上向下扔鹰蛋来确定 E 的.当鹰 ...

  8. 转载:jquery 对 Json 的各种遍历

    概述 JSON(javascript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式 ...

  9. shell分析nginx日志

    以下脚本都是基于上面日志格式的,如果你的日志格式不同需要调整awk后面的参数. 分析日志中的UserAgent cat access_20130704.log | awk -F "\&quo ...

  10. 小白学习Spark系列二:spark应用打包傻瓜式教程(IntelliJ+maven 和 pycharm+jar)

    在做spark项目时,我们常常面临如何在本地将其打包,上传至装有spark服务器上运行的问题.下面是我在项目中尝试的两种方案,也踩了不少坑,两者相比,方案一比较简单,本博客提供的jar包适用于spar ...