PAT--1092 To Buy or Not to Buy

用map计数即可

#include <iostream>
#include <map>
#include <string>

using namespace std;

map<char, int> dict;
int main()
{
    string str1, str2;
    while(cin >> str1 >> str2)
    {
        int cnt = 0;
        for(int i = 0; i < str2.size(); i++)
            dict[str2[i]]++;

        for(int j = 0; j < str1.size(); j++)
            if(dict.count(str1[j]) == 1 && dict[str1[j]] > 0)
                dict[str1[j]]--;

        bool flag = false;
        for(map<char, int>::iterator it = dict.begin(); it != dict.end(); it++)
            if(it->second != 0)
            {
                flag = true;
                cnt += it->second;
            }
        if(!flag)
            cout << "Yes " << str1.size()-str2.size() << endl;
        else
            cout << "No " << cnt << endl;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mch2869253130/article/details/87987546