字符串练习2 最长抑或路径(01trie树)
题目链接在这里:P4551 最长异或路径 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
是一道比较经典的问题,对于异或问题经常会使用01trie树来解决。
当然01trie树只是用来统计答案,往往还需要一些预处理操作。
比如此题需要把从当前点到根节点的路径的异或和求出来,讨论与根节点的联系在树的问题中很常见。
1 #include "bits/stdc++.h"
2 using namespace std;
3 const int MAX=1e6+5;
4 int n;
5 int tot,head[MAX],adj[MAX],nxt[MAX],wei[MAX];
6 int val[MAX];
7 void addedge(int u,int v,int w){
8 tot++;
9 adj[tot]=v;
10 wei[tot]=w;
11 nxt[tot]=head[u];
12 head[u]=tot;
13 }
14 void dfs(int x,int fa){
15 int i,j;
16 for (i=head[x];i;i=nxt[i]){
17 if (adj[i]==fa) continue;
18 val[adj[i]]=val[x]^wei[i];
19 dfs(adj[i],x);
20 }
21 }
22 struct Str{
23 int str[MAX][2],cnt;
24 bool vis[MAX];
25 Str(){
26 memset(str,0,sizeof(str));
27 cnt=0;
28 memset(vis,false,sizeof(vis));
29 }
30 void insert(int x){
31 int i,j,p=0;
32 bool zt;
33 for (i=30;i>=0;i--){
34 zt=(1<<i)&x;
35 if (str[p][zt]==0){
36 str[p][zt]=++cnt;
37 p=cnt;
38 }
39 else p=str[p][zt];
40 }
41 vis[p]=true;
42 }
43 int check(int v){
44 int i,j,p=0,ans=0;
45 bool zt;
46 for (i=30;i>=0;i--){
47 zt=v&(1<<i);
48 if (str[p][!zt]){
49 ans+=(1<<i);
50 p=str[p][!zt];
51 }
52 else p=str[p][zt];
53 // cout<<p<<endl;
54 }
55 // cout<<v<<' '<<ans<<endl;
56 return ans;
57 }
58 }ss;
59 int main(){
60 int i,j,u,v,w,ans=0;
61 scanf("%d",&n);
62 memset(head,0,sizeof(head));
63 memset(val,0,sizeof(val));
64 for (i=1;i<n;i++){
65 scanf("%d%d%d",&u,&v,&w);
66 addedge(u,v,w);
67 addedge(v,u,w);
68 }
69 dfs(1,0);
70 // for (i=1;i<=n;i++)
71 // cout<<i<<" : "<<val[i]<<endl;
72 for (i=1;i<=n;i++)
73 ss.insert(val[i]);
74 for (i=1;i<=n;i++)
75 ans=max(ans,ss.check(val[i]));
76 printf("%d\n",ans);
77 return 0;
78 }
字符串练习2 最长抑或路径(01trie树)的更多相关文章
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Leetcode 388.文件的最长绝对路径
文件的最长绝对路径 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示: dir ...
- Java实现 LeetCode 388 文件的最长绝对路径
388. 文件的最长绝对路径 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示 ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- 利用Manacher算法寻找字符串中的最长回文序列(palindrome)
寻找字符串中的最长回文序列和所有回文序列(正向和反向一样的序列,如aba,abba等)算是挺早以前提出的算法问题了,最近再刷Leetcode算法题的时候遇到了一个(题目),所以就顺便写下. 如果用正反 ...
- 51nod1274 最长递增路径
将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<alg ...
- 扩展KMP--求字符串S的所有后缀和字符串T的最长公共前缀
在解上面这个问题前我们要先解决一个类似的问题:求字符串s的所有后缀和s本身的最长公共前缀: 我们用next[]数组保存这些值: 现在我们假设要求next[ x ],并且next[ i ] 0<i ...
- POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...
- [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- LeetCode第十四题-字符串数组中最长的共同前缀
Longest Common Prefix 问题简介: 编写一个函数来查找字符串数组中最长的公共前缀字符串,如果没有公共前缀,则返回空字符串"" 举例: 1: 输入: [“xwq” ...
随机推荐
- Navie UI
开发文档 一个 Vue 3 组件库, 它比较完整,主题可调,用 TypeScript 写的,快.
- 2020ICPC上海I - Sky Garden
思维 [I-Sky Garden_第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(上海)(重现赛)@hzy0227 (nowcoder.com)](https://codeforces.co ...
- 什么时候用多线程什么时候用多进程呢?GUL
那么在 Python 中什么时候用多线程什么时候用多进程呢?当在CPU-bound(计算密集型:绝大多数时间在计算) 时最好用 - 多进程, 而在 I/O bound(I/O密集型 : IO 处理 并 ...
- Java jar打包成exe应用程序,可在无JDK/JRE环境下运行
转载自 https://blog.csdn.net/hao65103940/article/details/106494964 前期准备 一个jar包,没有bug能正常启动的jar包 exe4j,一个 ...
- 一、Java简单概述
一 . Java组成部分 JDK : java development kit (Java开发工具) JDK是提供给Java开发人员使用的,其中包含了java的开发工具,也 包括了JRE. 所以安装了 ...
- 056_Connect or Sync to your Salesforce database by using an external database
Integrate Salesforce with an External database: https://help.salesforce.com/articleView?id=000109985 ...
- ufun对象->NXopen
Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(SolidVector[i]))); std::vector<Face*& ...
- 天龙八部<三联版>三
虚竹经历这么多事情,只想回少林寺,在面馆遇到出来玩的阿紫被阿紫调戏而破了荤戒.随后丁春秋到来,抓到阿紫,恰好慕容复也来到面馆,二人相斗,阿紫在打斗过程中被丁春秋刺瞎双眼,但却被游坦之救走, 最后慕容复 ...
- spring-security-oauth2-authorization-server
旧依赖的移除 长久以来,使用Spring Security整合oauth2,都是使用Spring Security Oauth2这个系列的包: <dependency> <group ...
- 配置SASS过程
1.首先在电脑上安装Node.js 2.其次在命令行配置npm,命令:npm init.运行后会创建一个package.json文件 3.然后输入命令配置npm中可以使用SASS的包,命令:npm i ...