so easy

A - So easy
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 $a_1, a_2, a_3, \ldots, a_n$ - represents the content of the first file. The third line contains n integers $b_1, b_2, b_3, \ldots, b_n$ - represents the content of the second file.  
Process to the end of file.  
$1 \leq n \leq 100$  
$1 \leq a_i , b_i \leq 1000000000$
 

Output

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

Sample Input

     
     
3 1 1 2 1 2 2 4 5 3 7 7 7 5 3 3 4 2 5 2 3 2 5 2 5 3 1 2 3 1 2 4
 

Sample Output

     
     
YES YES NO NO 这道题我刚开始采用的是利用两个数组然后将重复的元素去掉,再进行比较。后来看了别人的简易代码得知:如果用set集合容器来做的话直接可以不用去重,因为set只能存入不重复的元素。 这是之前的代码比较繁琐;
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <list>
using namespace std;
int f(int a[],int n,int aa[])
{
    int i,j,k=0;

    for(i=0; i<n; i++)
        for(j=i+1; j<n; j++)
            if(a[i]==a[j])
                a[j]='@';
    for(i=0; i<n; i++)
        if(a[i]!='@')
            aa[k++]=a[i];
    return k;
}

int main()
{

    int n;
    while(cin>>n)
    {
        int a[1005];
        int s[1005];
        int t[1005];
        int t2[1005];
        int i,j,k,k1;
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<n; i++)
            cin>>s[i];
        k=f(a,n,t);
        k1=f(s,n,t2);
        int flag=1;
   if(k!=k1)
            cout<<"NO"<<endl;
        else
        {
            sort(t,t+k);
            sort(t2,t2+k1);
            for(i=0; i<k; i++)
            {

                if(t[i]!=t2[i])
                {
                    flag=0;
                }

            }

        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
        }


    }
    return 0;
}



以下是用的set代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main()
{int n;
while(~scanf("%d",&n))
{
    set<int >s1,s2;
    int i;
    for(i=0;i<n;i++)
    {int a;
        scanf("%d",&a);
        s1.insert(a);
    }
    for(i=0;i<n;i++)
    {
        int  b;
        scanf("%d",&b);
        s2.insert(b);
    }
    int flag=1;
    if(s1.size()!=s2.size())
    {
        flag=0;
    }
    else
    {
    set<int >::iterator it1,it2;
    for(it1=s1.begin(),it2=s2.begin();it1!=s1.end(),it2!=s2.end();it1++,it2++)
    {
        if(*it1!=*it2)
        {
            flag=0;
            break;
        }
    }
    }
    if(flag)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;

}
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值