PAT甲级1076~1100

1076 Forwards on Weibo (30 分)

Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

#include<iostream>
#include<vector>
using namespace std;

int N,L,K;
vector<int>fensi_of[1001];
int mindis[1001];

void dfs(int cur,int curlevel){
    if(curlevel>L)return;
    mindis[cur]=curlevel;
    curlevel++;
    for(int each:fensi_of[cur]){
        if(curlevel<mindis[each])
            dfs(each,curlevel);
    }
}

int main(){
    int i,j,k,l;
    cin>>N>>L;
    for(i=1;i<=N;i++){
        cin>>j;
        while(j--){
            cin>>k;
            fensi_of[k].emplace_back(i);
        }
    }
    cin>>K;
    while(K--){
        for(i=1;i<=N;i++)mindis[i]=1000000000;
        cin>>i;
        dfs(i,0);
        int count=0;
        for(i=1;i<=N;i++)
            if(mindis[i]!=1000000000)count++;
        cout<<count-1<<endl;
    }
}

1077 Kuchiguse (20 分)

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a preference is called “Kuchiguse” and is often exaggerated artistically in Anime and Manga.
For example, the artificial sentence ending particle “nyan~” is often used as a stereotype for characters with a cat-like personality:
Itai nyan~ (It hurts, nyan~)
Ninjin wa iyada nyan~ (I hate carrots, nyan~)
Now given a few lines spoken by the same character, can you find her Kuchiguse?

#include<iostream>
using namespace std;

int main(){
    string s[100];
    int N,i,j,k;
    cin>>N;
    getchar();
    for(i=0;i<N;i++)
        getline(cin,s[i]);
    for(i=1;;i++){
        for(j=0;j<N;j++)
            if(s[j].size()<i)break;
        if(j<N)break;
        bool signal=true;
        char c=s[0][s[0].size()-i];
        for(j=1;j<N;j++){
            char d=s[j][s[j].size()-i];
            if(d!=c)signal=false;
        }
        if(signal==false)break;
    }
    if(i==1)
        cout<<"nai";
    else{
        for(j=s[0].size()-(i-1);j<s[0].size();j++)
            cout<<s[0][j];
    }
}

1078 Hashing (25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

#include<iostream>
using namespace std;

int M,N;

bool isprime(int p){
    int i,j,k;
    if(p==1)return false;
    for(i=2;i*i<=p;i++)
        if(p%i==0)return false;
    return true;
}

int main(){
    int i,j,k,l,data[20000]={0};
    cin>>M>>N;
    while(isprime(M)==false)M++;
    for(l=0;l<N;l++){
        cin>>i;
        for(j=0;j<M;j++){
            k=(i+j*j)%M;
            if(data[k]==0)
                break;
            else continue;
        }
        if(j==M){
            if(l)cout<<" ";
            cout<<"-";
        }
        else{
            data[k]=i;
            if(l)cout<<" ";
            cout<<k;
        }
    }
}

1079 Total Sales of Supply Chain (25 分)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the total sales from all the retailers.

#include<iostream>
#include<vector>
using namespace std;

struct node{
    vector<int>child;
    int amount;
};

node n[100000];

int N;
double P,r;
double sum=0;

void dfs(int cur,double cur_danjia){
    if(n[cur].child.size()){
        cur_danjia*=(1+r/100);
        for(int each:n[cur].child)
            dfs(each,cur_danjia);
    }
    else{
        sum+=n[cur].amount*cur_danjia;
    }
}

int main(){
    int i,j,k;
    cin>>N>>P>>r;
    for(i=0;i<N;i++){
        cin>>j;
        if(j)
            while(j--){
                cin>>k;
                n[i].child.emplace_back(k);
            }
        else{
            cin>>k;
            n[i].amount=k;
        }
    }
    dfs(0,P);
    printf("%.1f",sum);
}

1080 Graduate Admission (30 分)

It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.
Each applicant will have to provide two grades: the national entrance exam grade GE ​ , and the interview grade GI ​ . The final grade of an applicant is (GE +G~I ​~ )/2. The admission rules are:
The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE ​ . If still tied, their ranks must be the same.
Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one’s turn to be admitted; and if the quota of one’s most preferred shcool is not exceeded, then one will be admitted to this school, or one’s other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.

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

int N,M,K;//N人数,M学校数,K志愿数

struct school{
    int quota,last;
    vector<int>admitted;
};

struct applicant{
    int id,Ge,Gi,G,prefer[5],rnk;
};

school s[100];
applicant a[40000];

bool cmp(applicant x,applicant y){
    if(x.G!=y.G)return x.G>y.G;
    return x.Ge>y.Ge;
}

int main(){
    cin>>N>>M>>K;
    int i,j,k,l;
    for(i=0;i<M;i++){
        cin>>s[i].quota;
    }
    for(i=0;i<N;i++){
        cin>>a[i].Ge>>a[i].Gi;
        a[i].G=a[i].Ge+a[i].Gi;
        for(j=0;j<K;j++){
            cin>>a[i].prefer[j];
        }
        a[i].id=i;
    }
    
    sort(a,a+N,cmp);
    for(i=0;i<N;i++){
        if(i==0||(a[i].G!=a[i-1].G||a[i].Ge!=a[i-1].Ge))a[i].rnk=i+1;
        else a[i].rnk=a[i-1].rnk;
    }
    
    for(i=0;i<N;i++){
        for(j=0;j<K;j++){
            k=a[i].prefer[j];
            if(s[k].admitted.size()<s[k].quota||a[i].rnk==a[s[k].last].rnk){
                s[k].admitted.emplace_back(a[i].id);
                s[k].last=i;
                break;
            }
        }
    }
    
    for(i=0;i<M;i++){
        sort(s[i].admitted.begin(),s[i].admitted.end());
        for(j=0;j<s[i].admitted.size();j++){
            if(j)cout<<" ";
            cout<<s[i].admitted[j];
        }
        cout<<endl;
    }
}

1081 Rational Sum (20 分)

Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.

#include<iostream>
using namespace std;

long long a[100],b[100],signal[100];

long long zdgys(long long x,long long y){
    long long big,small;
    if(x>y){
        big=x;
        small=y;
    }
    else{
        big=y;
        small=x;
    }
    while(small!=0){
        long long temp=big%small;
        big=small;
        small=temp;
    }
    return big;
}

long long zxgbs(long long x,long long y){
    long long temp=zdgys(x,y);
    return x*(y/temp);
}

int main(){
    long long i,j,k,l,N;
    cin>>N;
    for(i=0;i<N;i++){
        char c;
        cin>>a[i]>>c>>b[i];
        if(a[i]<0){
            a[i]*=-1;
            signal[i]=-1;
        }
        else signal[i]=1;
    }
    long long A,B;
    B=b[0];
    for(i=1;i<N;i++){
        B=zxgbs(B,b[i]);
    }
    A=0;
    for(i=0;i<N;i++){
        A+=signal[i]*a[i]*(B/b[i]);
    }
    long long temp=zdgys(A,B);
    A/=temp;
    B/=temp;
    if(A==0){
        cout<<0;
        return 0;
    }
    if(A<0){
        cout<<"-";
        A*=-1;
    }
    if(A>=B){
        cout<<A/B;
    }
    if(A>=B&&A%B){
        cout<<" ";
    }
    if(A%B){
        cout<<A%B<<"/"<<B;
    }
}

1082 Read Number in Chinese (25 分)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative.
For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

#include<iostream>
#include<vector>
using namespace std;

string c[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};

int main(){
    vector<string>ans;
    int N;
    cin>>N;
    if(N==0){
        cout<<"ling";
        return 0;
    }
    if(N<0){
        ans.emplace_back("Fu");
        N*=-1;
    }
    string s=to_string(N);
    int digit[12],i,j,k;
    for(i=0;i<s.size();i++){
        digit[s.size()-1-i]=s[i]-'0';
    }
    
    for(i=s.size()-1;i>=0;i--){
        if(i!=s.size()-1&&digit[i]>0){
            if(i!=7&&i!=3){
                if(digit[i+1]==0)ans.emplace_back("ling");
            }
            else{
                if(!digit[i+4]&&!digit[i+3]&&!digit[i+2]&&!digit[i+1]){
                    ans.emplace_back("ling");
                }
            }
        }
        if(digit[i]>0)ans.emplace_back(c[digit[i]]);
        if(i==8)ans.emplace_back("Yi");
        else if(i==4){
            if(digit[7]||digit[6]||digit[5]||digit[4]){
                ans.emplace_back("Wan");
            }
        }
        else{
            if(i%4==3&&digit[i])ans.emplace_back("Qian");
            else if(i%4==2&&digit[i])ans.emplace_back("Bai");
            else if(i%4==1&&digit[i])ans.emplace_back("Shi");
        }
    }
    for(i=0;i<ans.size();i++){
        if(i)cout<<" ";
        cout<<ans[i];
    }
}

1083 List Grades (25 分)

Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.

#include<iostream>
#include<algorithm>
using namespace std;

struct student{
    string name,ID;
    int grade;
};

int N;
student s[200];

bool cmp(student x,student y){
    return x.grade>y.grade;
}

int main(){
    int i,j,k;
    cin>>N;
    for(i=0;i<N;i++)
        cin>>s[i].name>>s[i].ID>>s[i].grade;
    int g1,g2;
    cin>>g1>>g2;
    sort(s,s+N,cmp);
    int count=0;
    for(i=0;i<N;i++){
        if(s[i].grade>=g1&&s[i].grade<=g2){
            cout<<s[i].name<<" "<<s[i].ID<<endl;
            count++;
        }
    }
    if(!count)
        cout<<"NONE";
}

1084 Broken Keyboard (20 分)

On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.
Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.

#include<iostream>
#include<vector>
using namespace std;

int main(){
    int broken[128]={0};
    vector<char>ans;
    string s1,s2;
    cin>>s1>>s2;
    int i,j,k;
    for(i=j=0;i<s1.size();){
        if(j<s2.size()&&s1[i]==s2[j]){
            i++;
            j++;
        }
        else{
            if(!broken[s1[i]]){
                broken[toupper(s1[i])]=1;
                broken[tolower(s1[i])]=1;
                ans.emplace_back(toupper(s1[i]));
            }
            i++;
        }
    }
    for(char c:ans){
        cout<<c;
    }
}

1085 Perfect Sequence (25 分)

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.
Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.

#include<iostream>
#include<algorithm>
using namespace std;

long long N,a[100000],p;

int main(){
    int i,j,k;
    cin>>N>>p;
    for(i=0;i<N;i++)cin>>a[i];
    sort(a,a+N);
    
    int maxlen=0;
    for(i=j=0;j<N;i++){
        while(j<N&&a[j]<=a[i]*p)j++;
        if(j-i>maxlen)maxlen=j-i;
    }
    cout<<maxlen;
}

1086 Tree Traversals Again (25 分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.请添加图片描述

#include<iostream>
#include<vector>
using namespace std;

struct node{
    int key;
    node *left,*right;
};

int N;
vector<int>v;

void makenode(node*&root){
    string s;
    int i;
    cin>>i;
    root=new node;
    root->key=i;
    cin>>s;
    if(s=="Push")
        makenode(root->left);
    else root->left=NULL;
    
    s="";
    cin>>s;
    if(s=="Push")
        makenode(root->right);
    else root->right=NULL;
}

void postordertravel(node*root){
    if(root==NULL)return;
    postordertravel(root->left);
    postordertravel(root->right);
    v.emplace_back(root->key);
}

int main(){
    cin>>N;
    string s;
    cin>>s;
    node*tree=NULL;
    makenode(tree);
    
    postordertravel(tree);
    for(int i=0;i<N;i++){
        if(i)cout<<" ";
        cout<<v[i];
    }
}

1087 All Roads Lead to Rome (30 分)

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;

int N,K,start=0,destination;//N城市数,K路数
unordered_map<string,int>M;
string namelist[200];
int happy_of_city[200],dis[200][200];
vector<int>v[200];
int mindis[200];
vector<int>path;
vector<int>final_path;
int final_dis,final_happy,final_step;
int num_of_drwleast_dis;

void dfs(int curcity,int curdis,int curhappy,int curstep){
    if(curdis>mindis[curcity])return;
    path.emplace_back(curcity);
    if(curcity==destination){
        if(curdis<mindis[curcity]){
            mindis[curcity]=curdis;
            final_path=path;
            final_dis=curdis;
            final_happy=curhappy;
            final_step=curstep;
            num_of_drwleast_dis=1;
        }
        else{
            num_of_drwleast_dis++;
            if(curhappy>final_happy||(curhappy==final_happy&&curstep<final_step)){
                final_path=path;
                final_dis=curdis;
                final_happy=curhappy;
                final_step=curstep;
            }
        }
    }
    else{
        mindis[curcity]=curdis;
        for(int each:v[curcity]){
            dfs(each,curdis+dis[curcity][each],curhappy+happy_of_city[each],curstep+1);
        }
    }
    path.pop_back();
}

int main(){
    int i,j,k,l;
    cin>>N>>K;
    string startcity;
    cin>>startcity;
    M[startcity]=0;
    namelist[0]=startcity;
    
    for(i=1;i<N;i++){
        cin>>namelist[i];
        M[namelist[i]]=i;
        cin>>happy_of_city[i];
    }
    
    destination=M["ROM"];
    while(K--){
        string city1,city2;
        cin>>city1>>city2>>l;
        i=M[city1];
        j=M[city2];
        v[i].emplace_back(j);
        v[j].emplace_back(i);
        dis[i][j]=dis[j][i]=l;
    }
    
    for(i=0;i<N;i++)mindis[i]=1000000000;
    dfs(start,0,0,0);
    
    cout<<num_of_drwleast_dis<<" "<<final_dis<<" "<<final_happy<<" "<<final_happy/final_step<<endl;
    cout<<startcity;
    for(i=1;i<final_path.size();i++){
        cout<<"->";
        cout<<namelist[final_path[i]];
    }
}

1088 Rational Arithmetic (20 分)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

#include<iostream>
using namespace std;

long long fenzi1,fenmu1,tag1;
long long fenzi2,fenmu2,tag2;
long long fenzi3,fenmu3,tag3;

long long zdgys(long long x,long long y){
    long long big,small;
    if(x>y){
        big=x;
        small=y;
    }
    else{
        big=y;
        small=x;
    }
    while(small!=0){
        long long temp=big%small;
        big=small;
        small=temp;
    }
    return big;
}

void simplify(long long &fenzi,long long &fenmu,long long &tag){
    if(fenzi==0)return;
    if(fenzi<0){
        tag=-1;
        fenzi*=-1;
    }
    else tag=1;
    long long temp=zdgys(fenzi,fenmu);
    fenzi/=temp;
    fenmu/=temp;
}

long long zxgbs(long long x,long long y){
    return x*(y/zdgys(x,y));
}

void print(long long fenzi,long long fenmu,long long tag){
    if(fenzi==0){
        cout<<0;
        return;
    }
    if(tag==-1)cout<<"(-";
    if(fenzi>=fenmu)cout<<fenzi/fenmu;
    if(fenzi>=fenmu&&fenzi%fenmu)cout<<" ";
    if(fenzi%fenmu)cout<<fenzi%fenmu<<"/"<<fenmu;
    if(tag==-1)cout<<")";
}

int main(){
    int i,j,k,l;
    char c;
    cin>>fenzi1>>c>>fenmu1;
    cin>>fenzi2>>c>>fenmu2;
    simplify(fenzi1,fenmu1,tag1);
    simplify(fenzi2,fenmu2,tag2);
    
    fenmu3=zxgbs(fenmu1,fenmu2);
    fenzi3=0;
    fenzi3+=tag1*fenzi1*(fenmu3/fenmu1);
    fenzi3+=tag2*fenzi2*(fenmu3/fenmu2);
    simplify(fenzi3,fenmu3,tag3);
    
    print(fenzi1,fenmu1,tag1);
    cout<<" + ";
    print(fenzi2,fenmu2,tag2);
    cout<<" = ";
    print(fenzi3,fenmu3,tag3);
    cout<<endl;//加法
    
    fenmu3=zxgbs(fenmu1,fenmu2);
    fenzi3=0;
    fenzi3+=tag1*fenzi1*(fenmu3/fenmu1);
    fenzi3-=tag2*fenzi2*(fenmu3/fenmu2);
    simplify(fenzi3,fenmu3,tag3);
    
    print(fenzi1,fenmu1,tag1);
    cout<<" - ";
    print(fenzi2,fenmu2,tag2);
    cout<<" = ";
    print(fenzi3,fenmu3,tag3);
    cout<<endl;//减法
    
    fenzi3=fenzi1*fenzi2*tag1*tag2;
    fenmu3=fenmu1*fenmu2;
    simplify(fenzi3,fenmu3,tag3);
    
    print(fenzi1,fenmu1,tag1);
    cout<<" * ";
    print(fenzi2,fenmu2,tag2);
    cout<<" = ";
    print(fenzi3,fenmu3,tag3);
    cout<<endl;//乘法
    
    fenzi3=fenzi1*fenmu2*tag1*tag2;
    fenmu3=fenmu1*fenzi2;
    if(fenmu3!=0)simplify(fenzi3,fenmu3,tag3);
    
    print(fenzi1,fenmu1,tag1);
    cout<<" / ";
    print(fenzi2,fenmu2,tag2);
    cout<<" = ";
    if(fenmu3==0)cout<<"Inf";
    else print(fenzi3,fenmu3,tag3);
    cout<<endl;//除法
}

1089 Insert or Merge (25 分)

According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.
Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

#include<iostream>
#include<algorithm>
using namespace std;

int N;
int a[100],b[100];
int work[100];

void Insertion(int k){//k表示有序部分的长度
    int i;
    for(i=0;i<N;i++)work[i]=a[i];
    sort(work,work+k);
}

void Merge(int k){//k表示块的大小
    int i;
    for(i=0;i<N;i++)work[i]=a[i];
    for(i=0;i<N;i+=k){//i表示每块的打头下标
        if(i+k<N)sort(work+i,work+i+k);
        else sort(work+i,work+N);
    }
}

bool issame(){
    int i;
    for(i=0;i<N;i++){
        if(b[i]!=work[i])return false;
    }
    return true;
}

int main(){
    int i,j,k;
    cin>>N;
    for(i=0;i<N;i++)cin>>a[i];
    for(i=0;i<N;i++)cin>>b[i];
    //数据输入
    
    for(k=N-1;k>0;k--){
        Insertion(k);
        if(issame()){
            Insertion(k+1);
            cout<<"Insertion Sort\n";
            for(i=0;i<N;i++){
                if(i)cout<<" ";
                cout<<work[i];
            }
            return 0;
        }
    }
    
    for(k=1;;k*=2){
        Merge(k);
        if(issame()){
            Merge(k*2);
            cout<<"Merge Sort\n";
            for(i=0;i<N;i++){
                if(i)cout<<" ";
                cout<<work[i];
            }
            return 0;
        }
    }
}

1090 Highest Price in Supply Chain (25 分)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

#include<iostream>
#include<vector>
using namespace std;

vector<int>v[100000];
int depth[100000];
int N;
double P,r;

void dfs(int cur){
    for(int each:v[cur]){
        depth[each]=depth[cur]+1;
        dfs(each);
    }
}

int main(){
    cin>>N>>P>>r;
    int i,j,k;
    int root;
    for(i=0;i<N;i++){
        cin>>j;
        if(j>=0)v[j].push_back(i);
        else root=i;
    }
    depth[root]=0;
    dfs(root);
    int maxdepth=0;
    for(i=0;i<N;i++)
        if(depth[i]>maxdepth)maxdepth=depth[i];
    int res=0;
    for(i=0;i<N;i++)
        if(depth[i]==maxdepth)res++;
    for(i=maxdepth;i--;)P*=(1+r/100);
    printf("%.2f %d",P,res);
    return 0;
}

1091 Acute Stroke (30 分)

One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.

#include<iostream>
#include<stack>
using namespace std;

struct three{
    int i,j,k;
    three(int _i,int _j,int _k){
        i=_i;j=_j;k=_k;
    }
};

int M,N,L,T;//M行数,N列数
int a[60][1286][128];
int b[60][1286][128];

int main(){
    cin>>M>>N>>L>>T;
    int i,j,k,l;
    for(i=0;i<L;i++){
        for(j=0;j<M;j++){
            for(k=0;k<N;k++)
                cin>>a[i][j][k];
        }
    }
    
    int ans=0;
    for(i=0;i<L;i++){
        for(j=0;j<M;j++){
            for(k=0;k<N;k++){
                if(a[i][j][k]==1&&!b[i][j][k]){
                    int count=0;
                    stack<three>S;
                    S.push(three(i,j,k));
                    b[i][j][k]=1;
                    while(S.size()){
                        three t=S.top();
                        S.pop();
                        int i,j,k;
                        i=t.i;j=t.j;k=t.k;
                        count++;
                        if(i-1>=0&&a[i-1][j][k]==1&&!b[i-1][j][k]){
                            S.push(three(i-1,j,k));
                            b[i-1][j][k]=1;
                        }
                        if(i+1<L&&a[i+1][j][k]==1&&!b[i+1][j][k]){
                            S.push(three(i+1,j,k));
                            b[i+1][j][k]=1;
                        }
                        if(j-1>=0&&a[i][j-1][k]==1&&!b[i][j-1][k]){
                            S.push(three(i,j-1,k));
                            b[i][j-1][k]=1;
                        }
                        if(j+1<M&&a[i][j+1][k]==1&&!b[i][j+1][k]){
                            S.push(three(i,j+1,k));
                            b[i][j+1][k]=1;
                        }
                        if(k-1>=0&&a[i][j][k-1]==1&&!b[i][j][k-1]){
                            S.push(three(i,j,k-1));
                            b[i][j][k-1]=1;
                        }
                        if(k+1<N&&a[i][j][k+1]==1&&!b[i][j][k+1]){
                            S.push(three(i,j,k+1));
                            b[i][j][k+1]=1;
                        }
                    }
                    if(count>=T)ans+=count;
                }
            }
        }
    }
    cout<<ans;
}

1092 To Buy or Not to Buy (20 分)

Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is Yes, please tell her the number of extra beads she has to buy; or if the answer is No, please tell her the number of beads missing from the string.
For the sake of simplicity, let’s use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.请添加图片描述

#include<iostream>
using namespace std;

int main(){
    int num[128]={0};
    string s1,s2;
    cin>>s1>>s2;
    for(char c:s1)num[c]++;
    int lack=0;
    for(char c:s2){
        if(num[c]>0)num[c]--;
        else lack++;
    }
    if(lack)cout<<"No "<<lack;
    else cout<<"Yes "<<s1.size()-s2.size();
}

1093 Count PAT’s (25 分)

The string APPAPT contains two PAT’s as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.
Now given any string, you are supposed to tell the number of PAT’s contained in the string.

#include<iostream>
using namespace std;

int main(){
    string S;
    cin>>S;
    int dp[3][S.size()];
    dp[0][0]=S[0]=='P'?1:0;
    dp[1][0]=0;
    dp[2][0]=0;
    for(int i=1;i<S.size();i++){
        if(S[i]=='P'){
            dp[0][i]=(dp[0][i-1]+1)%1000000007;
            dp[1][i]=dp[1][i-1];
            dp[2][i]=dp[2][i-1];
        }
        else if(S[i]=='A'){
            dp[0][i]=dp[0][i-1];
            dp[1][i]=(dp[1][i-1]+dp[0][i-1])%1000000007;
            dp[2][i]=dp[2][i-1];
        }
        else{
            dp[0][i]=dp[0][i-1];
            dp[1][i]=dp[1][i-1];
            dp[2][i]=(dp[2][i-1]+dp[1][i-1])%1000000007;
        }
    }
    cout<<dp[2][S.size()-1];
}
#include<iostream>
using namespace std;

int main(){
    string S;
    cin>>S;
    int numofP=0,numofPA=0,numofPAT=0;
    for(int i=0;i<S.size();i++){
        if(S[i]=='P'){
            numofP++;
            numofP%=1000000007;
        }
        else if(S[i]=='A'){
            numofPA+=numofP;
            numofPA%=1000000007;
        }
        else{
            numofPAT+=numofPA;
            numofPAT%=1000000007;
        }
    }
    cout<<numofPAT;
}

1094 The Largest Generation (25 分)

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.

#include<iostream>
#include<vector>
using namespace std;

vector<int>children[100];
int num_of_level[100]={0};

void dfs(int cur,int curlevel){
    num_of_level[curlevel]++;
    for(int each:children[cur]){
        dfs(each,curlevel+1);
    }
}

int main(){
    int N,M,i,j,k;
    cin>>N>>M;
    while(M--){
        cin>>i>>k;
        while(k--){
            cin>>j;
            children[i].push_back(j);
        }
    }
    
    dfs(1,1);
    
    int maxnum=-1,maxnumlevel;
    for(i=1;i<100;i++){
        if(num_of_level[i]>maxnum){
            maxnum=num_of_level[i];
            maxnumlevel=i;
        }
    }
    cout<<maxnum<<" "<<maxnumlevel;
}

1095 Cars on Campus (30 分)

Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.

#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<vector>
using namespace std;

struct record{
    int hh,mm,ss,totalss;
    string tag;
};

int N,K;
unordered_map<string,vector<record>>M;
unordered_map<string,int>M2;
char c;
int in_school[86400];
int out_school[86400];

bool cmp(record x,record y){
    return x.totalss<y.totalss;
}

int main(){
    int i,j,k,l;
    cin>>N>>K;
    for(i=0;i<N;i++){
        string car;
        record r;
        cin>>car>>r.hh>>c>>r.mm>>c>>r.ss>>r.tag;
        r.totalss=r.hh*3600+r.mm*60+r.ss;
        M[car].push_back(r);
    }
    
    for(auto it:M){
        auto A=it.second;
        sort(A.begin(),A.end(),cmp);
        for(i=0;i<A.size();){
            if(A[i].tag=="in"){
                if(i+1<A.size()&&A[i+1].tag=="out"){
                    in_school[A[i].totalss]++;
                    out_school[A[i+1].totalss]++;
                    M2[it.first]+=A[i+1].totalss-A[i].totalss;
                    i+=2;
                }
                else i++;
            }
            else i++;
        }
    }
    
    int car_num[86400]={0};
    for(i=0,j=0;i<86400;i++){
        j+=in_school[i];
        j-=out_school[i];
        car_num[i]=j;
    }
    
    for(i=0;i<K;i++){
        int hh,mm,ss;
        cin>>hh>>c>>mm>>c>>ss;
        int totalss=hh*3600+mm*60+ss;
        cout<<car_num[totalss]<<endl;
    }
    
    int maxtime=0;
    vector<string>maxcars;
    for(auto it:M2){
        if(it.second>maxtime){
            maxtime=it.second;
            maxcars={it.first};
        }
        else if(it.second==maxtime){
            maxcars.push_back(it.first);
        }
    }
    
    sort(maxcars.begin(),maxcars.end());
    
    for(string each:maxcars){
        cout<<each<<" ";
    }
    printf("%02d:%02d:%02d",maxtime/3600,maxtime/60%60,maxtime%60);
}

1096 Consecutive Factors (20 分)

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

#include<iostream>
#include<cmath>
using namespace std;

int N;
int main(){
    cin>>N;
    int i,j,k;
    int ans_start=N;
    int ans_length=1;
    for(i=2;i<=sqrt(N);i++){
        for(j=i,k=N;k%j==0;j++){
            k/=j;
        }
        if(j-i>ans_length||(j-i==ans_length&&i<ans_start)){
            ans_length=j-i;
            ans_start=i;
        }
    }
    cout<<ans_length<<endl;
    for(i=ans_start;i<ans_start+ans_length;i++){
        if(i>ans_start)cout<<"*";
        cout<<i;
    }
}

1097 Deduplication on a Linked List (25 分)

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

struct node{
    int key,next;
};

node n[100000];

int main(){
    int i,j,k,head,N;
    cin>>head>>N;
    while(N--){
        cin>>i>>j>>k;
        n[i].key=j;
        n[i].next=k;
    }
    
    bool appeared[10001]={0};
    vector<int>v1,v2;
    for(i=head;i>=0;i=n[i].next){
        j=abs(n[i].key);
        if(appeared[j]){
            v2.push_back(i);
        }
        else{
            appeared[j]=true;
            v1.push_back(i);
        }
    }
    
    for(i=0;i<v1.size();i++){
        if(i+1<v1.size())
            printf("%05d %d %05d\n",v1[i],n[v1[i]].key,v1[i+1]);
        else
            printf("%05d %d %d\n",v1[i],n[v1[i]].key,-1);
    }
    if(v2.size()){
        for(i=0;i<v2.size();i++){
            if(i+1<v2.size())
                printf("%05d %d %05d\n",v2[i],n[v2[i]].key,v2[i+1]);
            else
                printf("%05d %d %d\n",v2[i],n[v2[i]].key,-1);
        }
    }
}

1098 Insertion or Heap Sort (25 分)

According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.
Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

#include<iostream>
#include<algorithm>
using namespace std;

int N;
int origin[100],half[100];

void insert_iter(int result[],int k){
    int i;
    for(i=0;i<N;i++)result[i]=origin[i];
    sort(result,result+k);
}

int main(){
    int i,j,k;
    cin>>N;
    for(i=0;i<N;i++)cin>>origin[i];
    for(i=0;i<N;i++)cin>>half[i];
    
    for(i=N;i>=1;i--){
        int result[100];
        insert_iter(result,i);
        for(j=0;j<N;j++)
            if(result[j]!=half[j])break;
        if(j==N){
            cout<<"Insertion Sort\n";
            insert_iter(result,i+1);
            for(i=0;i<N;i++){
                if(i)cout<<" ";
                cout<<result[i];
            }
            return 0;
        }
    }
    cout<<"Heap Sort\n";
    
    int temp[100];
    for(i=0;i<N;i++)temp[i]=origin[i];
    sort(temp,temp+N);
    for(i=N-1;temp[i]==half[i];i--);
    
    for(j=0;j<N;j++)temp[j]=half[j];
    j=temp[0];
    temp[0]=temp[i];
    temp[i]=j;
    
    //0~i-1无序 i~N-1有序
    for(j=0;j<i-1;){
        if(j*2+1>=i)break;//左孩子出界
        if(temp[j*2+1]>temp[j])k=j*2+1;//左孩子大于根
        if(j*2+2<i)
            if(temp[j*2+2]>temp[k])k=j*2+2;//右孩子大于根
        if(k==j)break;
        int t=temp[j];
        temp[j]=temp[k];
        temp[k]=t;
        j=k;
    }
    
    for(i=0;i<N;i++){
        if(i)cout<<" ";
        cout<<temp[i];
    }
    return 0;
}

1099 Build A Binary Search Tree (30 分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than or equal to the node’s key. Both the left and right subtrees must also be binary search trees. Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.请添加图片描述

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

struct node{
    int data,lchild,rchild;
};

node n[1000];
int N,cursor=0;
int num[100];

void inorder_travel(int curID){
    if(n[curID].lchild>=0)inorder_travel(n[curID].lchild);
    n[curID].data=num[cursor++];
    if(n[curID].rchild>=0)inorder_travel(n[curID].rchild);
}

void level_travel(){
    queue<int>Q;
    Q.push(0);
    while(Q.size()){
        auto a=Q.front();
        Q.pop();
        if(a!=0)cout<<" ";
        cout<<n[a].data;
        if(n[a].lchild>=0)Q.push(n[a].lchild);
        if(n[a].rchild>=0)Q.push(n[a].rchild);
    }
}

int main(){
    cin>>N;
    int i,j,k;
    for(i=0;i<N;i++){
        cin>>j>>k;
        n[i].lchild=j;
        n[i].rchild=k;
    }
    
    for(i=0;i<N;i++)cin>>num[i];
    sort(num,num+N);
    inorder_travel(0);
    level_travel();
}

1100 Mars Numbers (20 分)

People on Mars count their numbers with base 13:
Zero on Earth is called “tret” on Mars. The numbers 1 to 12 on Earth is called “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” on Mars, respectively. For the next higher digit, Mars people name the 12 numbers as “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”, respectively. For examples, the number 29 on Earth is called “hel mar” on Mars; and “elo nov” on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.

#include<iostream>
using namespace std;

int N;
string gewei[13]={ "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
string shiwei[13]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int main(){
    cin>>N;
    while(N--){
        int num;
        if(scanf("%d",&num)==1){
            if(num<13)
                cout<<gewei[num]<<endl;
            else{
                if(num%13==0)
                    cout<<shiwei[num/13]<<endl;
                else cout<<shiwei[num/13]<<" "<<gewei[num%13]<<endl;
            }
        }
        else{
            string s1,s2;
            cin>>s2;
            if(getchar()==' '){
                s1=s2;
                cin>>s2;
                int i,j;
                for(i=0;i<13;i++)if(shiwei[i]==s1)break;
                for(j=0;j<13;j++)if(gewei[j]==s2)break;
                num=i*13+j;
                cout<<num<<endl;
            }
            else{
                int i;
                for(i=0;i<13;i++)if(gewei[i]==s2)break;
                if(i<13){
                    cout<<i<<endl;
                }
                else{
                    for(i=0;i<13;i++)if(shiwei[i]==s2)break;
                    cout<<i*13<<endl;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南宫萧幕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值