PAT Advanced Level 1039

题目地址http://pat.zju.edu.cn/contests/pat-a-practise/1039

思路分析:题目简单,唯一难点在于测试点5,容易超时

注意:不要使用iostream,会超时

C++版本(测试点5不通过)

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<int> stu[26][26][26][10];
int main()
{
    //freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin);
    for(int i=0;i<26;i++)
        for(int j=0;j<26;j++)
            for(int k=0;k<26;k++)
                for(int x=0;x<10;x++)
                    stu[i][j][k][x].clear();
    int n,k;
    cin>>n>>k;
    for(int i=0;i<k;i++){
        int cno,sno;
        cin>>cno>>sno;
        for(int j=0;j<sno;j++){
            string sname;
            cin>>sname;
            stu[sname.at(0)-'A'][sname.at(1)-'A'][sname.at(2)-'A'][sname.at(3)-'0'].push_back(cno);
        }
    }
    for(int i=0;i<n;i++){
        string sname;
        cin>>sname;
        vector<int> temp=stu[sname.at(0)-'A'][sname.at(1)-'A'][sname.at(2)-'A'][sname.at(3)-'0'];
        cout<<sname<<" "<<temp.size();
        sort(temp.begin(),temp.end());
        for(int j=0;j<temp.size();j++)
            cout<<" "<<temp[j];
        cout<<endl;
    }
    return 0;
}
C++的其他解决方案(二分查找)(测试点5没通过。。)

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct record{
    string sname;
    int cno;
    bool operator <(const record &A) const{
        if(A.sname==sname) return cno<A.cno;
        else return sname.compare(A.sname)<0;
    }
};
vector<record> s;
int getlow(string sname){
    int low=0;
    int high=s.size()-1;
    int middle=(low+high)/2;
    while(low<=high){
        if(sname.compare(s[middle].sname)==0) high=middle-1;
        else if(sname.compare(s[middle].sname)>0) low=middle+1;
        else high=middle-1;
        middle=(low+high)/2;
    }
    return middle;
}
int gethigh(string sname){
    int low=0;
    int high=s.size()-1;
    int middle=(low+high)/2;
    while(low<=high){
        if(sname.compare(s[middle].sname)==0) low=middle+1;
        else if(sname.compare(s[middle].sname)>0) low=middle+1;
        else high=middle-1;
        middle=(low+high)/2;
    }
    return middle;
}
int main()
{
    //freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin);
    s.clear();
    int n,k;
    cin>>n>>k;
    for(int i=0;i<k;i++){
        int cno,sno;
        cin>>cno>>sno;
        for(int j=0;j<sno;j++){
            record temp;
            cin>>temp.sname;
            temp.cno=cno;
            s.push_back(temp);
        }
    }
    sort(s.begin(),s.end());
    for(int i=0;i<n;i++){
        string sname;
        cin>>sname;
        int low=getlow(sname);
        if(sname.compare(s[low].sname)!=0) low++;
        int high=gethigh(sname);
        if(sname.compare(s[low].sname)!=0) high--;
        if(sname.compare(s[low].sname)!=0) cout<<sname<<" "<<0;
        else{
            cout<<sname<<" "<<high-low+1;
            for(int j=low;j<=high;j++)
                cout<<" "<<s[j].cno;
        }
        cout<<endl;
    }
    return 0;
}


C++版本(测试点全部通过)

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
vector<int> stu[26][26][26][10];
int main()
{
    //freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin);
    for(int i=0;i<26;i++)
        for(int j=0;j<26;j++)
            for(int k=0;k<26;k++)
                for(int x=0;x<10;x++)
                    stu[i][j][k][x].clear();
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        int cno,sno;
        scanf("%d%d",&cno,&sno);
        for(int j=0;j<sno;j++){
            char sname[5];
            scanf("%s",sname);
            stu[sname[0]-'A'][sname[1]-'A'][sname[2]-'A'][sname[3]-'0'].push_back(cno);
        }
    }
    for(int i=0;i<n;i++){
        char sname[5];
        scanf("%s",sname);
        vector<int> temp=stu[sname[0]-'A'][sname[1]-'A'][sname[2]-'A'][sname[3]-'0'];
        printf("%s %d",sname,temp.size());
        sort(temp.begin(),temp.end());
        for(int j=0;j<temp.size();j++)
            printf(" %d",temp[j]);
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值