简单问题大数据化,需要想办法存储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;
}
大数据存储优化与性能比较
本文探讨了在存储15000个数据时,如何通过map和hash方法进行优化,对比了使用string、cstring、链式哈希及map辅助的不同时间消耗、空间消耗和代码长度,最终选择了一种时间效率较高的居中方案。
242

被折叠的 条评论
为什么被折叠?



