洛谷P2786 英语1(eng1)- 英语作文——map

时间:2024-04-16 16:46:39

给一手链接 https://www.luogu.com.cn/problem/P2786

拿这道题当map模板练练手qwq

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<iostream>
using namespace std;
int read(){
    int ans=0,f=1,c=getchar();
    while(c<\'0\'||c>\'9\'){if(c==\'-\') f=-1; c=getchar();}
    while(c>=\'0\'&&c<=\'9\'){ans=ans*10+(c-\'0\'); c=getchar();}
    return ans*f;
}
int n,mod,ans;
char c;
string s;
map<string,int> q;
int pd(char c){
    if(c==\' \'||c==\',\'||c==\'.\'||c==\'!\'||c==\'?\') return 0;
    return 1;
}
int main(){
    n=read(); mod=read();
    for(int i=1;i<=n;i++) cin>>s,q.insert(pair<string,int>(s,read()));
    s="";
    while((c=getchar())!=EOF){
        if(pd(c)) s+=c;
        else ans=(ans+q[s])%mod,s="";
    }
    printf("%d\n",ans);
    return 0;
}
View Code