题解 | 代理服务器

代理服务器

https://www.nowcoder.com/practice/1284469ee94a4762848816a42281a9e0

#include <bits/stdc++.h>
using namespace std;
// 判断是否含0
bool all(vector<string>v,unordered_map<string,int>u){
    for(auto i:v){
        if(u[i]==0)return false;
    }
    return true;
}
// 重置所有为0,并设s为1
void reset(vector<string>v,unordered_map<string,int>&u,string s){
    for(auto i:v){
        u[i]=0;
    }
    u[s]=1;
}
int main() {
    int nums = 0;
    int n,m;
    vector<string> proxy;
    unordered_map<string,int> map;
    while(cin>>n){
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            proxy.push_back(s);
            map[s] = 0;
        }
        cin>>m;
        for(int i=0;i<m;i++){
            string s;
            cin>>s;
            if(map.find(s)==map.end()) continue;  //不考虑不和代理ip相同的服务器
            map[s]+=1; //标记出现次数
            if(all(proxy,map)){
                reset(proxy,map,s);
                nums+=1;  //加1
            }
        }
        if (proxy.size()==1){
            cout<< -1<<endl;
        }else{
            cout<<nums<<endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

狸猫换offer:神通广大的互联网
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务