A1039 Course List for Student (25 分)

一、技术总结

  1. 这里由于复杂度的限制,只能够使用vector,然后进行字符串转化:考虑到string、cin、cout会超时,可以使⽤用hash(262626*10+10)将学⽣生姓名变为int型,然后存储在vector里
  2. 这里出了一个巨大的问题,就是审题不清导致最后格式结果不正确。
  3. 还有就是空格输出问题,要注意前面是否已经有输出了。
  4. 还有就是字符串转化成int存储,hash列表的应用,空间换时间
int getID(char name[]){
    int id = 0;
    for(int i = 0; i < strlen(name); i++){
        id = 26*id + (name[i] - 'A');
    } 
    return id;   
}

二、参考代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
const int N = 40010;
const int M = 26*26*26*10 + 1;
vector<int> selectCourse[M];
int getID(char name[]){
    int id = 0;
    for(int i = 0; i < 3; i++){
        id = id*26 + (name[i] - 'A');
    }
    id = id*10 + (name[3] - '0');
    return id;
}
int main(){
    int n,k,id = 0;
    char name[5];
    cin >> n >> k;
    for(int i = 0; i < k; i++){
        int coursenum, number;
        scanf("%d%d", &coursenum, &number);
        for(int j = 0; j < number; j++){
            scanf("%s", name);
            id = getID(name);
            selectCourse[id].push_back(coursenum);
        }
    }
/*
    for(int i = 0; i < n; i++) {        
        scanf("%s", name);        
        id = getID(name);        
        sort(selectCourse[id].begin(),selectCourse[id].end());        
        printf("%s %lu", name, selectCourse[id].size());        
        for(int j = 0; j < selectCourse[id].size(); j++)            
            printf(" %d", selectCourse[id][j]);        
        printf("\n");    }    
    return 0;
*/
    char str[n+1][5];
    for(int i = 0; i < n; i++){
        scanf("%s", str[i]);
    }
    for(int i = 0; i < n; i++){
        int id = getID(str[i]);
        sort(selectCourse[id].begin(), selectCourse[id].end());
        printf("%s %d", str[i], selectCourse[id].size());
        for(int j = 0; j < selectCourse[id].size(); j++){
            //if(j != 0) printf(" ");
            printf(" %d", selectCourse[id][j]);
            
        }
        printf("\n");
    }
    return 0;

} 

猜你喜欢

转载自www.cnblogs.com/tsruixi/p/11906048.html
今日推荐