SDUT 1791 集合相等问题

版权声明:iQXQZX https://blog.csdn.net/Cherishlife_/article/details/86619225

集合相等问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

给定2 个集合S和T,试设计一个判定S和T是否相等的蒙特卡罗算法。
设计一个拉斯维加斯算法,对于给定的集合S和T,判定其是否相等。

Input

输入数据的第一行有1 个正整数n(n≤10000),表示集合的大小。接下来的2行,每行有n个正整数,分别表示集合S和T中的元素。

Output

将计算结论输出。集合S和T相等则输出YES,否则输出NO。

Sample Input

3
2 3 7
7 2 3

Sample Output

YES

Hint

Source
 

这个题用HashSet  和  TreeSet   都可以

set详解  看这两篇文章

https://blog.csdn.net/qq_33642117/article/details/52040345

https://www.cnblogs.com/yangliguo/p/7476788.html

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        Set set1 = new TreeSet();
        Set set2 = new TreeSet();

        for (int i = 0; i < n; i++) {
            set1.add(in.nextInt());
        }

        for (int i = 0; i < n; i++) {
            set2.add(in.nextInt());
        }
        if (set1.equals(set2))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}

/***************************************************
User name: jk180602
Result: Accepted
Take time: 192ms
Take Memory: 11872KB
Submit time: 2019-01-23 20:24:49
****************************************************/

猜你喜欢

转载自blog.csdn.net/Cherishlife_/article/details/86619225