Two Files

一、题目内容

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K

题目描述

Small W gets two files. There are n integers in each file. Small W wants to know whether these two files are same. So he invites you to write a program to check whether these two files are same. Small W thinks that two files are same when they have the same integer set.
For example file A contains (5,3,7,7),and file B contains (7,5,3,3). They have the same integer set (3,5,7), so they are same.
Another sample file C contains(2,5,2,5), and file D contains (2,5,2,3).
The integer set of C is (2,5),but the integer set of D is (2,3,5),so they are not same.
Now you are expected to write a program to compare two files with size of n.

输入描述

Multi test cases . Each case contain three lines. The first line contains one integer n represents the size of file. The second line contains n integers a1,a2,a3,…,an - represents the content of the first file. The third line contains n integers b1,b2,b3,…,bn - represents the content of the second file.
Process to the end of file.
1≤n≤1000000
1≤ai,bi≤1000000000

输出描述

For each case, output "YES" (without quote) if these two files are same, otherwise output "NO" (without quote).

示例1

输入

5
1 2 1 2 3
1 3 3 2 2
3
1 2 3
1 1 2

输出

YES
NO

二、解题思路

根据题干所知,两个文件是否相同,主要在于文件内的数组内的元素去重后数组是否相同,那么我们可以针对两个有序数组进行比较即可,只要有不相同的即输出“NO”。

代码实操

#include<bits/stdc++.h>
using namespace std;
int a[1000010],b[1000010];
int main() {
    int n;
    while(cin >> n) {
        for(int i = 0; i < n; i++) {
           cin >> a[i];
        }
        sort(a,a+n);
        for(int i = 0; i < n; i++) {
            cin >> b[i];
        }
        sort(b,b+n);
        int i = 0,j = 0;
        for(;i < n && j < n;) {
            if (a[i] != b[i]) break;
            i++;j++;
            while(i < n && a[i] == a[i - 1]) i++;
            while(j < n && b[j] == b[j - 1]) j++;
        }
        if (i == n && j == n) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

当然,以上代码还有可优化的空间,各位大佬可以留言🤺(抽空我也会提出我的优化方案,tip:重写排序算法)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,872评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,934评论 0 13
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,996评论 0 0
  • 我要吐槽
    明亮的写字桌阅读 187评论 0 0
  • 隔壁邻居家有一个很可爱的小姑娘,十三四岁,瓜子脸,大眼睛,小嘴巴,是标准的小美人胚子。 我妈让我去给邻居家送东西的...
    陈白九阅读 760评论 15 15

友情链接更多精彩内容