首页 > 其他分享 >易忘写法

易忘写法

时间:2023-03-04 15:57:48浏览次数:39  
标签:va cnt int next bit now 写法

由于电脑不常在身边,整理这些易忘用法,以备不时之需。

字典树(此为01Trie)工程写法

    struct Trie {
        int cnt;
        Trie* next[2];
    };
    typedef struct Trie* T;

    T NewTrie() {
        T tmp = (T)malloc(sizeof(Trie));
        tmp->cnt = 0;
        memset(tmp->next, 0, 2*sizeof(T));
        return tmp;
    }
    void Insert(T root, int x) {
        T now = root;
        int bit = 0;
        while (x) {
            if (now->next[x & 1] == NULL) {
                now->next[x & 1] = NewTrie();
            }
            now = now->next[x & 1];
            bit++;
            x >>= 1;
        }
        while (bit < 16) {
            if(!now -> next[0]) {
                now->next[0] = NewTrie();
            }
            now = now->next[0];
            bit++;
        }
        now -> cnt++;
    }

01trie数组写法

//leetcode https://leetcode.cn/problems/count-pairs-with-xor-in-a-range/submissions/393158437/
#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
    int search(int trie[],int num,int tgt){
        int p = 1;
        int cnt = 0;
        for (int i=14;i>=0;--i){
            int bit = ((num>>i)&1);
            p = p*2;
            if ((tgt>>i)&1){
                cnt+=trie[p+bit];
            }
            p += (bit^((tgt>>i)&1));
        }
        return cnt;
    }
    int countPairs(vector<int>& nums, int low, int high) {
        int trie[1<<16];
        memset(trie,0,sizeof(trie));
        int ans = 0;
        for (auto &num:nums){
            ans+=search(trie,num,high+1)-search(trie,num,low);
            int p = 1;
            for (int i=14;i>=0;--i){
                p = p*2+((num>>i)&1);
                ++trie[p];
            }
        }
        return ans;
    }
};
/*
处理异或问题的一种数据结构,01trie树,可用树状数组存储。
rt初值赋1,lson = 0son = rt<<1; rson = 1son = (rt<<1) + 1;
*/

 

sort lambda表达式自定义排序

vector<int> cmp;
cmp.push_back(2);
cmp.push_back(3);
sort(ve.begin(), ve.end(), [&](const vector<int> &va, const vector<int> &vb){
    for (auto x : cmp) {
        if (va[x] != vb[x]) {
            return va[x] < vb[x];
        }
    }
    return va[0] < vb[0];
});
/*
[&] 捕获的元素中,包括cmp
按照 va[2] < va[2] 排序
在按照 va[3] < vb[3] 排序
如果都失效,按照 va[0] < vb[0] 排序
输入ve:
5 5 5 5 5
1 2 4 4 3
6 6 3 3 2
0 0 3 2 1
排序后输出:

0 0 3 2 1 
6 6 3 3 2
1 2 4 4 3
5 5 5 5 5
*/
sort_with_lambda

 

结构体构造函数

struct poi {
    int x, y, z;
    poi();
    poi(int x, int y, int z) {
        this->x = x;
        this->y = y;
        this->z = z;
    }
};
// 用法举例:(que为优先队列)
que.push(poi(sx, sy, sz));

 

标签:va,cnt,int,next,bit,now,写法
From: https://www.cnblogs.com/zhonghuizaijian/p/17178424.html

相关文章

  • jdbcTemplate之“rowMapper内部类写法”
    jdbcTemplate的rowMapper内部类写法Stringsql="selectsku,featurefromproduct"List<Product>products=jdbcTemplate.query(sql,(rs,rowNum)->{......
  • js-- 数组中取最大值的三种写法
    js数组取最大值方法有哪些Math.max()letarr=[3,1,2,4,6,0,19];console.log(Math.max(...arr))//19functionmathGetMax(arr){returnMath.max.apply(null......
  • go 语言 写法
    为了很久之后能快速回顾GO的写法特意记录一下1:go可以隐式声明  :=  (可以不跟类型) 类似   var变量名变量类型  变量名:=变量值(声明时go会自动判断......
  • VUE2 完整写法
    自定义指令的生命周期钩子bind()、inserted()、updata()<inputtype="text"v-fbind:value="n"/><script>directives:{fbind:{            ......
  • vue双向绑定和双向修改写法总结
    2.x双向绑定//使用value和input老式写法<ChildComponentv-model="pageTitle"/>//是以下的简写<ChildComponent:value="pageTitle"@input="pageTitle=$event"/......
  • shell 1+2+3+....+100的最简单写法
    #!/usr/bin/envbashexportPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binsum=0while((sum<=100))doleta=a+sumletsum++doneech......
  • shell 遍历目录大小的经典写法
    #!/usr/bin/envbashexportPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binread-p"请输入目录名称(例如:/root)"resultcd${result}forkin$(ls${re......
  • 同一地址,不同库的写法
    (`uat_xwsc_mysql_data`.`t_union_order``o`JOIN`uat_xwsc_mysql_data`.`t_union_product``p`ON((`o`.`UNION_PRODUCT_ID`=`p`.`ID`))) LEFTJOIN`uat_xwsc......
  • ES6-ES11 对象的简化写法
    视频<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>......
  • String集合拼多个or,模糊查询。mybatis-plus-构造器的写法
    List<String>list=newArrayList<>();QueryWrapper<Object>queryWrapper=newQueryWrapper<>();queryWrapper.and(CollUtil.isNotEmpty(list),qw->{list.fo......