比赛 2024暑假C班集训8 评测结果 AAAAAAAAAA
题目名称 单词默写 最终得分 100
用户昵称 darkMoon 运行时间 1.367 s
代码语言 C++ 内存使用 48.85 MiB
提交时间 2024-07-08 10:04:18
显示代码纯文本
#include<bits/stdc++.h> 
using namespace std;
//#define fin cin
//#define fout cout
ifstream fin("engzam.in");
ofstream fout("engzam.out");
auto mread = [](){
    int x;
    fin >> x;
    return x;
};
const int N = 2e5 + 5;
int n = mread(), m = mread(), ans[N], idx, sum[N * 10], to[N * 10][30], now;
struct node{
    int op, t, id;
    string s;
}s[N];
void add(string s){
    int x = 0;
    for(int i = 0; i < s.size(); i ++){
        if(to[x][s[i] - 'a'] == 0){
            to[x][s[i] - 'a'] = ++now;
        }
        x = to[x][s[i] - 'a'];
        sum[x] ++;
    }
    return;
}
int query(string s){
    int x = 0;
    for(int i = 0; i < s.size(); i ++){
        if(to[x][s[i] - 'a'] == 0){
            return 0;
        }
        x = to[x][s[i] - 'a'];
    }
    return sum[x];
}
signed main(){
    string tmp;
    for(int i = 1, x; i <= n; i ++){
        fin >> tmp >> x;
        idx ++;
        s[idx].op = 1, s[idx].t = x, s[idx].id = i, s[idx].s = tmp;
    }
    for(int i = 1, x; i <= m; i ++){
        fin >> tmp >> x;
        idx ++;
        s[idx].op = 2, s[idx].t = x, s[idx].id = i, s[idx].s = tmp;
    }
    sort(s + 1, s + 1 + idx, [](node a, node b){
        if(a.t == b.t)
        return a.op < b.op;
        return a.t > b.t;
    });
    for(int i = 1; i <= idx; i ++){
        if(s[i].op == 1){
            add(s[i].s);
        }
        else{
            ans[s[i].id] = query(s[i].s);
        }
    }
    for(int i = 1; i <= m; i ++){
        fout << ans[i] << "\n";
    }
    return 0;
}