[HDU](5058)So easy --去重

来源:互联网 发布:怎样重新申请淘宝账号 编辑:程序博客网 时间:2024/05/22 00:18

So easy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1268    Accepted Submission(s): 684


Problem Description
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.
 

Input
Multi test cases (about 100). 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.
1n100
1ai,bi1000000000
 

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

Sample Input
31 1 21 2 245 3 7 77 5 3 342 5 2 32 5 2 531 2 31 2 4
 

Sample Output
YESYESNONO
自己曾经在参加一个学院小竞赛时,遇到过去重的问题,当时困扰了好久,然后通过学习,在csdn论坛上看到有前辈写了一个去重的算法,十分好理解,觉得不错,所以在做杭电这道题是,用到了,反馈还不错。听说这道题还可以用C++ 的STL做,自己能力还不到,还要努力学习~o(〃'▽'〃)o
题意:给你两个数字序列,去重后,比较相不相等。
思路:先进行排序,然后去重。 
#include<iostream>#include<algorithm>using namespace std;int main(){    int n;    int a[100];    int b[100];    int i;    int j;    while(cin>>n){    int c[100];    int d[100];    int nc=0;    int nd=0;    int count=0;    for(i=0;i<n;i++)        cin>>a[i];    for(i=0;i<n;i++)        cin>>b[i];    sort(a,a+n);    sort(b,b+n);    for(i=0;i<n;i++)    {        for(j=i+1;j<n;j++)        {            if(a[i]==a[j])                a[j]=-1;        }    }    for(i=0;i<n;i++)    {        for(j=i+1;j<n;j++)        {            if(b[i]==b[j])                b[j]=-1;        }    }    for(i=0;i<n;i++)    {        if(a[i]!=-1)        {            c[nc]=a[i];            nc++;        }    }    for(i=0;i<n;i++)    {        if(b[i]!=-1)        {            d[nd]=b[i];            nd++;        }    }    for(i=0;i<nd;i++)    {        if(c[i]==d[i])            count++;    }    if(count==nc && count==nd)        cout<<"YES"<<endl;    else        cout<<"NO"<<endl;    }    return 0;}



0 0
原创粉丝点击