字典树

本文介绍了一种解决今日头条异或问题的方法,通过构建字典树来高效计算所有两两元素异或结果中大于给定值的数量。同时,还探讨了K-th Smallest in Lexicographical Order及Word Search II两个问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今日头条  异或

题目描述

给定整数m以及n各数字A1,A2,..An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果,请求出这些结果中大于m的有多少个。

输入描述:

第一行包含两个整数n,m. 

第二行给出n个整数A1,A2,...,An。

数据范围

对于30%的数据,1 <= n, m <= 1000

对于100%的数据,1 <= n, m, Ai <= 10^5

输出描述:

输出仅包括一行,即所求的答案

示例1

输入

3 10  
6 5 10

输出

2

思路:用整数的位来构造字典树。

import java.util.*;
public class Main{
    static class TireTree{
        TireTree[] next = new TireTree[2];
        int count = 1;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        sc.nextLine();
        int[] a = new int[n];
        for(int i = 0;i<n;i++){
            a[i] = sc.nextInt();
        }
        System.out.println(helper(a,m));
    }
    
    public static long helper(int[] a,int m){
        TireTree tree = buildTree(a);
        long result = 0;
        for(int i = 0;i<a.length;i++){
            result+=slove(a[i],m,tree,31);
        }
        return result/2;
    }
    
    public static long slove(int num,int m,TireTree tree,int index){
        if(tree == null) return 0;
        int aDight = (num>>index)&1;
        int mDight = (m>>index)&1;
        if(aDight == 1&&mDight == 1){
            return slove(num,m,tree.next[0],index-1);
        }else if(aDight == 0&&mDight == 1){
            return slove(num,m,tree.next[1],index-1);
        }else if(aDight == 1&&mDight == 0){
            long p = tree.next[0]==null?0:tree.next[0].count;
            long q = slove(num,m,tree.next[1],index-1);
            return p+q;
        }else{
            long p = tree.next[1] == null?0:tree.next[1].count;
            long q = slove(num,m,tree.next[0],index-1);
            return p+q;
        }
    }
    
    public static TireTree buildTree(int[] a){
        TireTree tree = new TireTree();
        for(int i = 0;i<a.length;i++){
            TireTree cur = tree;
            for(int j = 31;j>=0;j--){
                int d = (a[i]>>j)&1;
                if(cur.next[d] == null){
                    cur.next[d] = new TireTree();
                }else{
                    cur.next[d].count++;
                }
                cur = cur.next[d];
            }
        }
        return tree;
    }
}

440. K-th Smallest in Lexicographical Order

 思路:以当前数字开头的数的个数和k比较

class Solution {
    public int findKthNumber(int n, int k) {
        int cur = 1;
        k--;
        while(k>0){
            int len = helper(n,cur);
            if(len>k){
                k--;
                cur*=10;
            }else{
                cur++;
                k-=len;
            }
        }
        return cur;
    }
    
    public int helper(int n,long n1){
        int len = 0;
        long n2 = n1+1;
        while (n1 <= n) {
            len += Math.min(n + 1, n2) - n1;
            n1 *= 10;
            n2 *= 10;
        }
        return len;
    }
}

212. Word Search II

 

class Solution {
    public List<String> findWords(char[][] board, String[] words) {
        List<String> list = new ArrayList<>();
        TrieNode root = buildTree(words);
        for(int i = 0;i<board.length;i++){
            for(int j = 0;j<board[0].length;j++){
                helper(board,i,j,root,list);
            }
        }
        return list;
    }
    
    public void helper(char[][] board,int i,int j,TrieNode root,List<String> list){
        if(i<0||j<0||i==board.length||j==board[0].length) return;
        int index = board[i][j] - 'a';
        if(index>25||index<0) return;
        if(root.next[index] == null) return;
        root = root.next[index];
        if(root.str!=null){
            list.add(root.str);
            root.str = null;
        }
        board[i][j]^=256;
        helper(board,i-1,j,root,list);
        helper(board,i,j-1,root,list);
        helper(board,i+1,j,root,list);
        helper(board,i,j+1,root,list);
        board[i][j]^=256;
    }
    
    public TrieNode buildTree(String[] words){
        TrieNode root = new TrieNode();
        for(String s:words){
            TrieNode p = root;
            for(char c:s.toCharArray()){
                int index = c-'a';
                if(p.next[index] == null) p.next[index] = new TrieNode();
                p = p.next[index];
            }
            p.str = s;
        }
        return root;
    }

    
}

class TrieNode{
    TrieNode[] next;
    String str;
    public TrieNode(){
        next = new TrieNode[26];
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值