其他分享
首页 > 其他分享> > HDU 2648 Shopping(字符串哈希)

HDU 2648 Shopping(字符串哈希)

作者:互联网

目录

链接

HDU 2648 shopping - https://acm.hdu.edu.cn/showproblem.php?pid=2648

分析

代码

/* hdu 2648 shopping */
#include<bits/stdc++.h>
using namespace std;
#define MAXN 10010
#define N 10000
int BKDRHash(char sn[]){
    unsigned int key = 0;
    while(*sn)
        key = key*131+(*sn++);
    return key%N;
}
char ns[MAXN][32];
int n, m, ps[MAXN];
vector<int> ls[N];
int main(){
    int mp, tp, key, x, y, ans;
    char tn[32];
    while(scanf("%d", &n) == 1){        
        for(int i = 1; i < N; ++i) ls[i].clear();
        memset(ps, 0, sizeof ps);
        for(int i = 1; i <= n; ++i){
            scanf("%s", ns[i]);
            x = BKDRHash(ns[i]);
            ls[x].push_back(i);
        }
        scanf("%d", &m);
        while(m--){
            for(int i = 1; i <= n; ++i){
                ans = 0;
                scanf("%d %s", &tp, tn);
                x = BKDRHash(tn);
                for(int j = 0; j < ls[x].size(); ++j){
                    y = ls[x][j];
                    if(j+1 == ls[x].size() || strcmp(tn, ns[y]) == 0){
                        ps[y] += tp;
                        if(strcmp(tn, "memory") == 0) mp = ps[y];
                        break;
                    }
                }
            }
            for(int i = 1; i <= n; ++i) if(mp < ps[i]) ans++;
            printf("%d\n", ans+1);
        }
    }
    return 0;
}

标签:2648,ps,HDU,Shopping,int,scanf,tn,ls,key
来源: https://blog.csdn.net/jpphy0/article/details/118751228