【题解】 HDU 3572 SPY

该博客解析了一个名为HDU 3572 SPY的编程题目,涉及间谍名单的处理。内容包括题目描述、输入输出格式、样例测试以及题意分析。博主指出,关键在于处理三份名单的交集,并按原顺序输出,但应注意避免使用可能导致排序的操作,如使用关联容器MAP。最后,提供了错误代码和正确AC代码供读者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 

题目描述

题意分析

AC代码


题目描述

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem Description

The National Intelligence Council of X Nation receives a piece of credible information that Nation Y will send spies to steal Nation X’s confidential paper. So the commander of The National Intelligence Council take measures immediately, he will investigate people who will come into NationX. At the same time, there are two List in the Commander’s hand, one is full of spies that Nation Y will send to Nation X, and the other one is full of spies that Nation X has sent to Nation Y before. There may be some overlaps of the two list. Because the spy may act two roles at the same time, which means that he may be the one that is sent from Nation X to Nation Y, we just call this type a “dual-spy”. So Nation Y may send “dual_spy” back to Nation X, and it is obvious now that it is good for Nation X, because “dual_spy” may bring back NationY’s confidential paper without worrying to be detention by NationY’s frontier So the commander decides to seize those that are sent by NationY, and let the ordinary people and the “dual_spy” in at the same time .So can you decide a list that should be caught by the Commander?
A:the list contains that will come to the NationX’s frontier.
B:the list contains spies that will be sent by Nation Y.
C:the list contains spies that were sent to NationY before.

Input

There are several test cases.
Each test case contains four parts, the first part contains 3 positive integers A, B, C, and A is the number which will come into the frontier. B is the number that will be sent by Nation Y, and C is the number that NationX has sent to NationY before. 
The second part contains A strings, the name list of that will come into the frontier.
The second part contains B strings, the name list of that are sent by NationY.
The second part contains C strings, the name list of the “dual_spy”.
There will be a blank line after each test case.
There won’t be any repetitive names in a single list, if repetitive names appear in two lists, they mean the same people.

Output

Output the list that the commander should caught (in the appearance order of the lists B).if no one should be caught, then , you should output “No enemy spy”

Sample Input

8 4 3

Zhao Qian Sun Li Zhou Wu Zheng Wang

Zhao Qian Sun Li

Zhao Zhou Zheng

2 2 2

Zhao Qian

Zhao Qian

Zhao Qian

Sample Output

Qian Sun Li

No enemy spy

 

题意分析

题意:某国潜入间谍,现在给你三份表单,间谍就在 1 和 2 的交集   没有在3中出现的。

            一开始的思路是使用关联容器MAP,通过操纵键值来达到目的,然而它需要按照表序输出

            MAP会对关键字进行排序,就WA了,也可以通过重载运算符来达到目的,但在下才疏学浅,

            欢迎大佬在评论下方实现。

            同时注意find函数在<algorithm> 而不是<string>中的

            返回第一个与所查询值相同的迭代器,如果没找到则返回end迭代器

 

最下方有AC代码

 

错误代码

#include <iostream>
#include <map>
#include <string>
using namespace std;

map <string ,int> s;
map <string ,int> :: iterator it;

int main()                             //  此份代码不能AC ,提供思路耳
{
    int n,m,k,sign,mark,i,p;
    string r;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        s.clear();
        sign=0;mark=0;p=0;
        for(i=0;i<n;i++)
        {
            cin>>r;
            s[r]++;
        }
        for(i=0;i<m;i++)
        {
            cin>>r;
            s[r]=-1;
        }
        for(i=0;i<k;i++)
        {
            cin>>r;
            s[r]=1;
        }
        for(it=s.begin(); it!=s.end();it++)
        {
            if( it->second == -1)
            {
                if(sign==1) cout<<' ';
                cout<< it->first ;
                sign=1;
                mark=1;
            }
        }
        if(mark==0) cout<<"No enemy spy"<<endl;
        else cout<<endl;
    }
    return 0;
}

 

 

 

 

AC代码

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

vector <string> x,y,z,s;
int main()
{
    int n,m,k,i;
    string r;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        x.clear();y.clear();z.clear();s.clear();
        for(i=0;i<n;i++)
        {
            cin>>r;
            x.push_back(r);
        }
        for(i=0;i<m;i++)
        {
            cin>>r;
            y.push_back(r);
        }
        for(i=0;i<k;i++)
        {
            cin>>r;
            z.push_back(r);
        }

        for(i=0;i<m;i++)
        {
            if(find(x.begin(),x.end(),y[i])!=x.end())     // 注意
            if(find(z.begin(),z.end(),y[i])==z.end())

            s.push_back(y[i]);
        }
        if(s.size()==0)
        {
            cout<<"No enemy spy"<<endl;
        }
        else
        {
            for(i=0;i<s.size();i++)
            {
                cout<<s[i];
                if(i!=s.size()-1)
                {
                    cout<<' ';
                }
                else
                {
                    cout<<endl;
                }
            }
        }
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值