PAT b 1065_pat

Source: Internet
Author: User
1065. Single dogs (25)

"Single Dog" is a nickname for single people in Chinese. Ask you to find a single guest from a large party of tens of thousands of people to give special care.

Input format:

Enter the first row to give a positive integer n (<=50000), is the logarithm of a known couple/partner, then n rows, each row gives a couple/partner-for convenience, each corresponds to an ID number, 5 digits (from 00000 to 99999), separated by a space between IDs ; then give a positive integer m (<=10000), the total number of people attending the party; the next line gives the ID of the M-guest, separated by a space. The title guarantees no bigamy or foot-stepping on two ships.

Output format:

First line of the first row of the total number of guests; then the second line lists the guests in order of ID increment. The IDs are separated by 1 spaces, and there must be no extra spaces between the lines. Input Sample:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
Output Sample:
5
10000 23333 44444 55555 88888

Resolution

This question is the last question in Serie B, the meaning of this topic is to output single, in the right to do not have to output, just do this problem when I would like to use an array, write to the half of the discovery can not write down, and then suddenly think of the structure may also seem to be able to do before the time is done with the structure of the results of the time to do so, Changed for a long time did not change well, then saw a big guy's approach to a problem, found that the array do more simple time complexity and space complexity than my small. First, define two arrays A and b,b to store their wives or husbands. And then enter the number of numbers, and the number of people to come, here need to use two tags, a array of storage if a is to give him a value of 1, the input order is from the go, the first time to give A's object 1, If A's object comes in, give a 0 to show that he's not a single dog. The value of array A is actually only 0,1,-1. If the value of an array element is 1, it means a single dog.

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main () {
    int N;
    cin>>n;
    int i;
    int a[100000]={0};
    int b[100001]={100000};
    int a,b;
    for (i=0;i<n;i++) {
        scanf ("%d%d", &a,&b);
        b[a]=b;
        b[b]=a;
    }
    int count=0;
    int M;
    cin>>m;
    for (i=0;i<m;i++) {
        cin>>a;
        if (a[a]==0) {
            count++;
            A[a]=1;
            a[b[a]]=-1;
        }
        else if (a[a]==-1) {
            a[b[a]]=0;
            count--
        }
    }
    int flag=0,j=0;
    cout<<count<<endl;
    for (i=0;j<count;i++) {
        if (a[i]==1) {
            if (flag==0)
            flag=1;
            else
            cout<< "";
        printf ("%05d", I);
        j + +;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.