题解 | #记票统计#

记票统计

http://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894

map和vector双管齐下
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

int main() {
    int n1;
    while(cin>>n1){
       vector<string> v(n1);
        map<string,int> m;
        m.insert(make_pair("Invalid", 0));
        string temp;
        for(int i = 0;i<n1;i++){
            cin>>temp;
            v[i]=temp;
            m.insert(make_pair(temp, 0));
            
        }
        v.push_back("Invalid");
        int n2;
        cin>>n2;
        for(int j = 0;j<n2;j++){
            cin>>temp;
            auto it=m.find(temp);
            if(it!=m.end())m[temp]++;
            else m["Invalid"]++;
        }
        for(auto it = v.begin();it!=v.end();it++){
            cout<<(*it)<<' '<<':'<<' '<<m[(*it)]<<endl;
        }
        
    }
}

全部评论

相关推荐

中信银行 AI算法岗 29~32w
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务