51Nod 1095 Anigram单词——map

传送门

解析:两个map,一个存入排序后的单词和数量,一个存原单词和数量。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>

using namespace std;

#define IOS         cin.tie(0) , cout.sync_with_stdio(0)

const int INF = 0x3f3f3f3f;

int n;
string s;
map<string,int> mp;     //排序后的单词
map<string,int> mpt;    //原单词

int main()
{
    IOS;
    string temp;

    cin >> n;
    while(n--){
        cin >> s;
        temp = s;
        sort(s.begin(),s.end());

        if(!mp.count(s))  mp[s] = 1;
        else    mp[s]++;
        if(!mpt.count(temp))  mpt[temp] = 1;
        else    mpt[temp]++;
    }

    cin >> n;
    while(n--){
        cin >> s;
        temp = s;
        sort(s.begin(),s.end());

        if(!mp.count(s)){
            cout << 0 << endl;
        }
        else{
            if(mpt.count(temp)){
                cout << mp[s] - mpt[temp] << endl;
            }else{
                cout << mp[s] << endl;
            }
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40526226/article/details/85297998