PTA class B 1100 school celebration (25 points)

Posted by phpvolution on Thu, 06 Jan 2022 15:57:10 +0100

Zhejiang University will celebrate its 122nd anniversary in 2019. In order to prepare for the anniversary, Alumni Association collects all alumni ID number. Now you need to write a program to identify all the alumni according to the ID number of all the people who attend the celebration.

Input format:

The input is given a positive integer N of not more than 105 in the first line, followed by N row, and each row gives an alumni ID number (18 bit string consisting of numbers and upper case X). The title ensures that the ID number is not repeated.

Then, the information of all the people who came to celebrate the ceremony is given: first, a positive integer M of no more than 105, followed by M row, and each row gives the ID number of a person. The title ensures that the ID number is not repeated.

Output format:

First, output the number of alumni participating in the school anniversary on the first line. Then output the oldest alumni ID card in the second line - note that ID number 7-14 gives the birthday of yyyymmdd format. If there is no alumni, the ID number of the oldest guests will be exported in the second row. The title guarantees that such alumni or guests must be the only one.

Input example:

5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042

Input example:

3
150702193604190912

Problem solving ideas:

The whole question is simple. First, store the alumni ID number as alumni library. If the ID number is followed by the alumni library, it is alumni, otherwise it is not. The age is judged by the ID number representing the birthday's substring 7-14, the youngest being the oldest and the oldest. In order to count the oldest ID number, two 7-14 digit 99999999 ID number str3 and str4 are defined as the oldest alumni and others. Then read the ID card number that needs to be judged in turn. If the number is in alumni library, the number of alumni will be +1, and if the ID card's birthday is less than the first alumni born, if the ID number is less than, then the ID number will be used to update str3. If not, alumni only judge whether the ID card's birthday is less than the other person who is born at the earliest date. If less than, then use the ID number to update str4.

Difficulty: mainly timeout.

The original code test points 3 and 4 always timeout. In order to solve this problem, the following efforts have been made:

1. Change cin to scanf (no effect, at least at known test points);

2. A boolean flag is set to judge whether there are alumni. The initial value is false. If the first alumni is encountered, the value is changed to true. The main purpose is that when an alumni is encountered and then a non alumni is encountered, you don't have to judge whether it is the oldest of the non alumni and don't care, Anyway, as long as there is one alumni, it's OK to find the oldest alumni (this is whether I can reduce the judgment times of if, but it mainly depends on how many alumni are included in the given test example. If it's all alumni, it's useless)

3. at the beginning, I used vector<string> storage, find search, always reported timeout, vector find time complexity O(n), map time complexity was O(logn), so I converted to map storage alumni library, because there was no other operations such as deleting alumni library, no need for iterator, so alumni Library used map<string, map storage, ID number. bool is true, that is, if string is an alumni, map[string] returns true, otherwise it returns false.

Attach code

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<functional>
using namespace std;
int main(){
    int numa,numb,num=0;
    cin>>numa;
    bool flag=false;
    //vector<string> s1;
    map<string,bool> s1;
    string str3="372928999999998710",str4="372928999999998710";
    for(int i=0;i<numa;i++){
        string tmp;
        tmp.resize(18);
        scanf("%s",&tmp[0]);
        //s1.push_back(tmp);
        s1[tmp]=true;
    }
    cin>>numb;
    for(int i=0;i<numb;i++){
        string tmp;
        tmp.resize(18);
        scanf("%s",&tmp[0]);
        cout<<tmp<<" "<<s1[tmp]<<endl;
        if(!flag){
           if(!s1[tmp]){
                if(str4.substr(6,8)>tmp.substr(6,8)){
                  str4=tmp;
                }
           }
          else {
              num++;
              if(str3.substr(6,8)>tmp.substr(6,8)){
                 str3=tmp;
              } 
              flag=true;
            }
        }
       else if(s1[tmp]){
            if(str3.substr(6,8)>tmp.substr(6,8)){
                str3=tmp;
            }
            num++;
        }
        
    }
    if(flag){
        cout<<num<<endl;
        cout<<str3<<endl;
    }else{
        cout<<num<<endl;
        cout<<str4<<endl;
    }
    return 0;
}

Topics: Algorithm