华为笔试题:查找兄弟单词

输入描述:

 

先输入字典中单词的个数,再输入n个单词作为字典单词。
输入一个单词,查找其在字典中兄弟单词的个数
再输入数字n

输出描述:

 

根据输入,输出查找到的兄弟单词的个数

示例1

输入

3 abc bca cab abc 1

输出

2
bca
#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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值