文章标题

这里写图片描述
这里写图片描述
这里写图片描述
输入描述:

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

输出描述:

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

输入例子:

3
abc
bca
cab
abc
1

输出例子:

2
bca
这个其实很简单
就有一些细节要注意,别看错题,注意输入输出条件即可

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int number(const string &str1);//获得字符串的排好序后的权值
int main(){
    int n;
    while(cin>>n){
        map<pair<string,int>,int>   temp;
        //利用map的特性直接排序,懒得写排序了
        string str="";
        while(n--){
            cin>>str;
            temp[pair<string,int>(str,number(str))]++;
        }
        string target = "",first_str="";
        int res,len,count=0;
        cin>>target>>res;
        auto itr = temp.begin();
        len = number(target);
        while(itr!=temp.end()){
            //获取符合条件的字符串的数量
            if(itr->first.first.size()==target.size()&&itr->first.second==len&&itr->first.first!=target){
                ++count;
                int i = itr->second;
                while(i>1){
                //如果输入重复数据,就要计数多次
                    ++count;
                    i--;
                    if(--res==0)
                        first_str = itr->first.first;
                }
                if(--res==0)
                    first_str = itr->first.first;
            }
            itr++;
        }
        //输出结果,如果找到的数不够目标的数的位置大就不输出
        cout<<count<<endl;
        if(first_str!="")
            cout<<first_str<<endl;
    }
    return 0;
}
int number(const string &str1) {
    string str = str1;
    sort(str.begin(),str.end());
    int res=0;
    auto itr = str.begin();
    while (itr != str.end()) {
        res = res*10 + (*itr - 'a');
        itr++;
    }
    return res;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值