Huawei Written Questions: Find Brother Words

Enter description:

 

Enter the number of words in the dictionary first, then enter n words as dictionary words.
Enter a word, find the number of brother words in the dictionary,
then enter the number n

Output description:

 

According to the input, output the number of found brother words

Example 1

Input

3 abc bca cab abc 1

Output

2 
parts
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    int n;//单词个数
    while (cin >> n) {
        vector<string> s;
        for (int i = 0; i < n; ++i) {
            string ss;
            cin >> ss;
            s.push_back(ss);
        }
        sort(s.begin(), s.end());
        string str;//待查询的单词
        int index;//第几个兄弟单词
        cin >> str;
        cin >> index;
        map<char, int> mi;
        for (int i = 0; i < str.size(); ++i) {
            mi[str[i]]++;
        }
        int num = 0;
        string ans = "";
        for (int i = 0; i < n; ++i) {
            map<char, int> m;
            if (s[i] != str) {
                for (int j = 0; j < s[i].size(); ++j) {
                    m[s[i][j]]++;
                }
//                map<char,int>::iterator it;
//                for (it = m.begin(); it != m.end() ; ++it) {
//                    cout << it->first << "->" << it->second << endl;
//                }
                if (m == mi) {
                    num++;
                    if (index == num) ans = s[i];
                }
            }
        }
        cout << num << endl;
        if(num >= index) cout << ans << endl;
        else cout << "" << endl;
    }
    return 0;
}

 

Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/105187118