Sicily 1818. 成绩转换

大数据存储优化与性能比较
本文探讨了在存储15000个数据时,如何通过map和hash方法进行优化,对比了使用string、cstring、链式哈希及map辅助的不同时间消耗、空间消耗和代码长度,最终选择了一种时间效率较高的居中方案。

简单问题大数据化,需要想办法存储15000个数据,另外还要考虑个中可能重复的情况。简单暴力的话估计(没试过)会超时,我考虑了使用map和hash的两种做法:对于前者前者,若是用string存储学生姓名,耗时1.56s,若是用cstring存储学生姓名,耗时0.46s;对于后者(链式哈希),若是直接存储,耗时0.19s,若是用map来辅助,耗时0.26s。这四种情况,时间消耗和空间消耗、代码长度成反比。在这里贴出的是居中方案,即0.26s的那种情况。

Run Time: 0.26sec

Run Memory: 1096KB

Code length: 1104Bytes

SubmitTime: 2012-01-27 17:40:41

// Problem#: 1818
// Submission#: 1200739
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
#include <string>
#include <map>
using namespace std;

int hashing( char s[] ) {
    int key = 0;
    for ( int i = 0; s[ i ] != '\0'; i++ )
        key += s[ i ];
    return key % 500;
}

int main()
{
    int T;
    int n, m;
    char name[ 100 ];
    int score;

    scanf( "%d", &T );
    while ( T-- ) {
        map<string, int> data[ 500 ];
        scanf( "%d%d", &n, &m );
        while ( n-- ) {
            scanf( "%s%d", name, &score );
            data[ hashing( name ) ][ name ] = score;
        }

        while ( m-- ) {
            scanf( "%s", name );
            score = data[ hashing( name ) ][ name ];
            if ( score < 0 || score > 100 )
                printf( "Score is error!\n" );
            else if ( score < 60 )
                printf( "E\n" );
            else if ( score < 70 )
                printf( "D\n" );
            else if ( score < 80 )
                printf( "C\n" );
            else if ( score < 90 )
                printf( "B\n" );
            else if ( score <= 100 )
                printf( "A\n" );
        }
    }

    return 0;

}                                 


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值