首页 > 其他分享 >Day_1(并查集朋友圈、字典序排序)

Day_1(并查集朋友圈、字典序排序)

时间:2022-09-07 20:22:08浏览次数:83  
标签:String int 查集 Day public 朋友圈 str new 节点

1.并查集

朋友圈:找出最多的一个圈子内有多少用户!

  • id[](表示当前节点的父节点)
  • nodeNum[] (表示当前节点为根的那一组节点数量)
import java.util.Scanner;

//并查集
class UnionFind{
    int[] id;   //表示当前结点的父节点
    int[] nodeNum; //表示当前节点为根的那一组的节点数量
    int[] height; //表示当前节点为跟的那一组的节点数高度
    int num; //连通集的数目

    public UnionFind(int n){
        num = n;
        id = new int[n];
        nodeNum = new int[n];
        height = new int[n];

        for(int i=0;i<n;i++){
            id[i] = i;
            nodeNum[i] = 1;
            height[i] = 1;
        }
    }

    public int find(int p){
        while(p != id[p]){
            p = id[p];
        }
        return p;
    }

    public void Union(int p,int q){
        int i = find(p);
        int j = find(q);
        if(i!=j){
            if(height[i]>height[j]){
                id[j] = i;
                nodeNum[i] += nodeNum[j];
            }else if(height[i]<height[j]){
                id[i] = j;
                nodeNum[j] += nodeNum[i];
            }else{
                id[i] = j;
                nodeNum[j] += nodeNum[i];
                height[j]++;
            }
            num--;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int times = sc.nextInt();
        while (times > 0) {
            int n = sc.nextInt();
            UnionFind uf = new UnionFind(10000001);
            for(int i=0;i<n;i++){
                int a = sc.nextInt();
                int b = sc.nextInt();
                uf.Union(a,b);
            }
            int max = 1;
            for(int i=0;i<uf.nodeNum.length;i++){
                if(max<uf.nodeNum[i]){
                    max = uf.nodeNum[i];
                }
            }
            System.out.println(max);
            times--;
        }
    }

}

2.按字典序排序的第k小子串

1、找出所有字串

2、字串按照字典序排序

3、找出第k小的字串

import java.util.Scanner;

//字典序第k小的字串
class Heap{
    String[] str;
    public  Heap(int n){
        str = new String[n];
    }
    //比较大小函数
    public boolean CmpStr(String str1,String str2){
        int len1 = str1.length();
        int len2 = str2.length();
        int i =0;
        while(len1>0&&len2>0){
            if(str1.charAt(i)<str2.charAt(i)) return true;
            if(str1.charAt(i)>str2.charAt(i)) return false;
            len1--;
            len2--;
            i++;
        }
        if(len2>0) return true;
        else return false;
    }


    //调整大根堆函数
    public void HeadAdjust(int k,int len){
        str[0] = str[k];
        for(int i=2*k;i<=len;i*=2){
            if(i<len && CmpStr(str[i],str[i+1])) i++;
            if (CmpStr(str[i],str[0])) break;
            else{
                str[k] = str[i];
                k=i;
            }
        }
        str[k] = str[0];
    }

    public void BuildMaxHeap(int len){
        for(int i=len/2;i>0;i--){
            HeadAdjust(i,len);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();
        Heap hs = new Heap(100);
        int k=0;
        int count = 0;
        boolean p=true;

        for(int len = 1; len <= n; len ++) {
            for (int i = 0; i < str.length() - len + 1; i++) {
                String substr = str.substring(i, i + len);
                if(k>0){
                    for(int j=0;j<k;j++){
                        if(substr.equals(hs.str[j+1])) p=false;
                    }
                }

                if(p){
                    if(count<n){
                        k++;
                        count++;
                        hs.str[k] = substr;
                        hs.BuildMaxHeap(k);
                    }else{
                        if(hs.CmpStr(substr,hs.str[1])){
                            hs.str[1] = substr;
                            hs.BuildMaxHeap(k);
                        }
                    }
                }else{
                    p = true;
                }
            }
        }
        System.out.println(hs.str[1]);
    }
}
//aaddsfsddsf
//5
//aadds

标签:String,int,查集,Day,public,朋友圈,str,new,节点
From: https://www.cnblogs.com/kinghau/p/16667153.html

相关文章

  • Day 3
    今天做一两道题然后学java或者俄罗斯方块第二遍萌新密码#4  既然这么提示了,首先依据末尾的两个=这应该是base64,先解码  base编码常用有  85、91、92分......
  • 学习python-Day56
    今日学习内容补充:JSON知识点JSON是JavaScript(JavaScriptObjectNotation)是轻量级的文本数据交换的格式,JSON解析器和JSON支持许多不同的编程语言。独立于其......
  • Python学习-Day4
    (1)变量的命令规则命名规则:增加代码的识别与可读性定义时=两边留空格每个单词都是用小写,多个单词使用_连接驼峰命名法:小驼峰式命名法:第一个单词的首字母小写,后续单词首......
  • 前端JS-Day22
    箭头函数不创建this对象!图片无缝衔接:保证轮播图到最后直接跳转到第一位。 进行轮播图自动播放的时候,可以采取手动调用点击事件的方式操作。window.addEventListene......
  • 跟着黑马学SSM——Day5之Spring事务
    Spring事务简介事务作用:在数据层保障一系列的数据库操作同成功同失败Spring事务作用:在数据层或业务层一系列的数据库操作同成功同失败案例:银行转账需求:实现任意两......
  • DAY 252 Python定时任务
    在日常工作中,我们常常会用到需要周期性执行的任务,一种方式是采用Linux系统自带的crond结合命令行实现。另外一种方式是直接使用Python。接下来整理的是常见的Python定......
  • [Python以终为始]Day 2–在VSCode开发
    [Python以终为始]Day2–在VSCode开发想研究机器学习的前端工程师,从零到一百学习python的笔记前置下载并安装VSCode在VSCode安装由微软开发的python套件准备开始!......
  • Java零基础入门学习Day[2]
    了解Java的基本语法1.代码的书写格式每条功能执行语句的结尾都要加上';'严格区分大小写代码简洁美观,可读性强2.代码的注释单行注释  '//注释内容'    ......
  • 算法养成计划--day2
    200220906刷题第一个很简单,第二个评论区有人用求余公式,学到了https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/solution/mian-shi-ti-58-ii-zuo-xuan-z......
  • Hive-day3
    Hive分区 在大数据中,最常见的一种思想就是分治,我们可以把大的文件切割划分成一个个的小的文件,这样每次操作一个个小的文件就会很容易了,同样的道理,在hive当中也是支持这种......