首页 > 试题广场 > 查找兄弟单词
[编程题]查找兄弟单词

输入描述:

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



输出描述:

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

示例1

输入

3	abc	bca	cab	abc	1

输出

2	bca
这个题目不难。是问题的描述让人很是糊涂。
正确的描述应该是这样的:
输入描述:
先输入字典中单词的个数n,再输入n个单词作为字典单词。
再输入一个单词,查找其在字典中兄弟单词的个数m
再输入数字k

输出描述:
根据输入,输出查找到的兄弟单词的个数m
然后输出查找到的兄弟单词的第k个单词。

编辑于 2016-08-30 20:48:33 回复(14)

尼玛,出题的语文水平简直服了。一道破题,坑比要求还多。
1.输出不是一行,而是两行,分别是数量及对应的单词。
2.要判断一下,如果兄弟单词列表为空或者输入的数字大于列表的长度,都不进行输出。。。
3.强行把简单题目难度上升到困难,你们开心就好。
python解法如下:

from collections import defaultdict

while True:
    try:
        dd = defaultdict(list)
        a = input().split()
        # words是输入的单词,lookup是要查找的单词,num是要查找兄弟单词的索引,brothers是找到的兄弟单词列表
        words, lookup, num, brothers = a[1:1 + int(a[0])], a[-2], int(a[-1]), []
        for i in words:
            dd["".join(sorted(i))].append(i)
        for i in dd["".join(sorted(lookup))]:
            if i != lookup: brothers.append(i)
        # 下面这两行坑的老子调了半个小时。
        print(len(brothers))
        if brothers and num <= len(brothers):
            print(sorted(brothers)[num - 1])
    except:
        break
编辑于 2017-09-08 09:39:32 回复(2)
//调试了很久,通过50%,因为忽视了一个关键点,字典元素一定要排序
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool isBrother(string str, string s){    
    if(str.size() == s.size()){
        if(str == s)
        	return false;
		sort(str.begin(), str.end());
        sort(s.begin(), s.end());
        if(str == s)
        	return true;
    }
    return false;
}
int main(){
    int num;
    while(cin >> num){
        string str;
        string word,s;
        int index;
        vector<string> vs;
        for(int i = 0; i < num; ++i){
            cin >> str;
            vs.push_back(str);
        }  
        sort(vs.begin(), vs.end());  // 因为是字典,一定要排序!!
        cin >> word;
        cin >> index;
        int counts = 0;
        
        for(int i = 0; i < num; ++i){
            if(isBrother(word, vs[i])){
                counts ++;
                if(counts == index)
                    s = vs[i];
            }
        } 
        if(!vs.empty())
        	cout << counts << endl;
        if(counts >= index)
        	cout << s << endl;  
       
    }
    return 0;
}

发表于 2017-03-26 21:23:38 回复(9)
谁能告诉我这是什么情况
输出跟要求一样 说我不对



测试用例:
473 bb daccb caddd bddc c baa adb ad abbcb cdaa abab a abcc ddcbd cadcc cdacd aaa a b acccd bbb dacc cc acbdd bcbb ba bacaa adda acd aaad d ab acac bc dabab abcd aacba aba daa bb ad cddab a bbaa aacad cdac acbcc cada bacd adcad cdadc bcbcc aa b acd cbaac ddcd ccb dac a dac adbcb bcda dda a ab ca dd d abd a dbb ccabd bdddd abd adc aaa baccb ccdcd a da c cadc dcdbd d aa bb a cac c ad adb ca cdcc cadd dddca d cba cb caab caa dd cd bca abc cdaa cdb bad dad bda d ddbc dab baaa adaac b a dbccd bd b bdad cdacd baa ac ddcad d bb acc aa cd cbdbb bbbcb a cc aacc c aadc dbcd a bca dd abbb ccdcd ccd ab d a a dadcd dbd bcaa c cda b ddab caaaa cdcdb b acbc ccaa dabca dcd b ba dbcc da bdbcc ab abaca bb cddc caca da dadba accdd bdac dbcd bcbbd ab bd ccb ddaa aa b b d bddd cabac aaba ab ccdb db abbd ada bdadb c abba dd cdb bca cccda badba abaa ac aabad db ccbad bddd ada dba acba b bc dd bbbbd cc cbdd cd abcaa bb ddaca acadb bbbb bddcc bdada aaa bcbda c aaa aadd cdddc adb cdbab c cbca bb aacab acdb bbdab b acbda cbdcd bda bacdc db d adcbd bccc c aaa cdd bdcd bac a aaab ddbb cd ccdbb addcc cdc c ca baadc addba dbdbd dba bbdda bcb c cdad aacac dcada cb aaad a cccab caca aad bbb dd b babbb cba bdaca db bacd bc bcbda cdda bcccd bdcda bdbcd adb cbcb a c bacba abbb adab adab b b d bca badbc baa cdb b abc aabb b d c cdab cacda d cdcda adcdc bcc bbccd b adb caba cbaaa aadb dcc add bcac bacbd bb a b c cabaa c caad c aa bcc ccab ddc dadca cdcba aaba dabbd dcb a bddb bb a c c cbc ccd dd a cabbb b caadb cb dca cbb ddaa bcadc dab a bbda cd bc ccad bbd ab c acddd cdd dbbbb daaab abbb cabc add ca caa bbbb dcab daaaa baca dcd ccacb ba bddaa acac dbcc bcc cbbcc b abba daa dbab bcca ba aa d dcdc d dcaa cbcda bd b ccbcb baa dcacd d c cbda baba d abb c cbdc da dbbb cd aabc dadc b a ddb c ddd ccdc ccd cba dbaac dcccd ddbac dbbdd bad bcdd cb dac dccd d a acdd d c cb b bcbb c a aca bcba d d bbdbc d c dabad ccca dc adb ddb dcdd dba ad ddaba c ac ddac bbbd cd a dacbb 1 
对应输出应该为:
6 adbcb 
你的输出为:
6 adbcb

发表于 2016-10-09 22:01:37 回复(14)
想了好久没做对,于是惹不住请大神出手,大神几分钟搞定,智商碾压,热腾腾的答案来了
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

vector<string> dict;

bool is_friend(string a, string b) {
if (a == b) return false;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
return a == b;
}

int main() {
int n;
while (scanf("%d", &n) == 1) {
dict.clear();

for (int i = 0; i < n; i++) {
string s;
cin >> s;
dict.push_back(s);
}
sort(dict.begin(), dict.end());

string ask;
cin >> ask;
int id;
scanf("%d", &id);


int cnt = 0;
string res = "";
for (int i = 0; i < (int)dict.size(); i++) {
if (is_friend(dict[i], ask)) {
cnt++;
if (cnt == id) {
res = dict[i];
}
}
}

printf("%d\n", cnt);
if (cnt >= id)
printf("%s\n", res.c_str());
}
return 0;
}

编辑于 2016-04-20 21:47:00 回复(11)
哪位大神帮我看看为啥总是报数组越界呀
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);		
		int amount = Integer.parseInt(sc.nextLine());
		String[] strings = new String[amount];
		for(int i=0;i<amount;i++){
			strings[i] = sc.nextLine();
		}
		String parent = sc.nextLine();
		int findBroStrIndex = sc.nextInt();
		int broAmount = 0;//兄弟单词个数
		List<String> brotherWords = new ArrayList<String>();
		Arrays.sort(strings);//排序
		char[] paretnChar = parent.toCharArray();
		Arrays.sort(paretnChar);
		for(String s:strings){
			char[] sTemp = s.toCharArray();
			Arrays.sort(sTemp);
			if(Arrays.equals(paretnChar,sTemp) & !s.equals(parent)){
				broAmount++;
				brotherWords.add(s);
			}
		}
		if(amount >= findBroStrIndex){
			System.out.println(broAmount+"\n"+brotherWords.get(findBroStrIndex-1));
		}		
		sc.close();
	}
}


发表于 2016-08-20 15:54:54 回复(5)
这是个什么玩意?明明是对的,说不对

测试用例:
473 bb daccb caddd bddc c baa adb ad abbcb cdaa abab a abcc ddcbd cadcc cdacd aaa a b acccd bbb dacc cc acbdd bcbb ba bacaa adda acd aaad d ab acac bc dabab abcd aacba aba daa bb ad cddab a bbaa aacad cdac acbcc cada bacd adcad cdadc bcbcc aa b acd cbaac ddcd ccb dac a dac adbcb bcda dda a ab ca dd d abd a dbb ccabd bdddd abd adc aaa baccb ccdcd a da c cadc dcdbd d aa bb a cac c ad adb ca cdcc cadd dddca d cba cb caab caa dd cd bca abc cdaa cdb bad dad bda d ddbc dab baaa adaac b a dbccd bd b bdad cdacd baa ac ddcad d bb acc aa cd cbdbb bbbcb a cc aacc c aadc dbcd a bca dd abbb ccdcd ccd ab d a a dadcd dbd bcaa c cda b ddab caaaa cdcdb b acbc ccaa dabca dcd b ba dbcc da bdbcc ab abaca bb cddc caca da dadba accdd bdac dbcd bcbbd ab bd ccb ddaa aa b b d bddd cabac aaba ab ccdb db abbd ada bdadb c abba dd cdb bca cccda badba abaa ac aabad db ccbad bddd ada dba acba b bc dd bbbbd cc cbdd cd abcaa bb ddaca acadb bbbb bddcc bdada aaa bcbda c aaa aadd cdddc adb cdbab c cbca bb aacab acdb bbdab b acbda cbdcd bda bacdc db d adcbd bccc c aaa cdd bdcd bac a aaab ddbb cd ccdbb addcc cdc c ca baadc addba dbdbd dba bbdda bcb c cdad aacac dcada cb aaad a cccab caca aad bbb dd b babbb cba bdaca db bacd bc bcbda cdda bcccd bdcda bdbcd adb cbcb a c bacba abbb adab adab b b d bca badbc baa cdb b abc aabb b d c cdab cacda d cdcda adcdc bcc bbccd b adb caba cbaaa aadb dcc add bcac bacbd bb a b c cabaa c caad c aa bcc ccab ddc dadca cdcba aaba dabbd dcb a bddb bb a c c cbc ccd dd a cabbb b caadb cb dca cbb ddaa bcadc dab a bbda cd bc ccad bbd ab c acddd cdd dbbbb daaab abbb cabc add ca caa bbbb dcab daaaa baca dcd ccacb ba bddaa acac dbcc bcc cbbcc b abba daa dbab bcca ba aa d dcdc d dcaa cbcda bd b ccbcb baa dcacd d c cbda baba d abb c cbdc da dbbb cd aabc dadc b a ddb c ddd ccdc ccd cba dbaac dcccd ddbac dbbdd bad bcdd cb dac dccd d a acdd d c cb b bcbb c a aca bcba d d bbdbc d c dabad ccca dc adb ddb dcdd dba ad ddaba c ac ddac bbbd cd a dacbb 1 
对应输出应该为:
6 adbcb 
你的输出为:
6 adbcb
发表于 2017-05-09 14:44:10 回复(4)
1.没有兄弟单词时,只输出一个0;
2.兄弟单词索引超过兄弟单词总数时,只输出兄弟单词的总数
发表于 2016-11-30 12:28:53 回复(2)
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;


public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			int num = in.nextInt();
			String[] s = new String[num];
			int count = 0;
			for(int i = 0;i<num;i++){
				s[i] = in.next();
			}
			String key = in.next();
			char[] keyChar = key.toCharArray();
			Arrays.sort(keyChar);
			int no = in.nextInt();//第几个
			ArrayList<String> list = new ArrayList<String>();
			for(int i = 0;i<num;i++){
				int c = check(key,s[i],keyChar);
				count += c;
				if(c==1)
					list.add(s[i]);
			}
			System.out.println(count);
			Collections.sort(list);
			if(count>=no)
				System.out.println(list.get(no-1));
		}
	}
	private static int check(String key,String word,char[] keyChar){
		if(key.equals(word)||key.length()!=word.length())
			return 0;
		char[] wordChar = word.toCharArray();
		Arrays.sort(wordChar);
		return Arrays.equals(keyChar, wordChar)?1:0;
	}
}

发表于 2016-05-26 10:12:36 回复(3)
//朋友们,这道题有2个坑,不怪大家不AC,就是出题人没说清楚(插会腰,可把自己牛逼坏了)
//1.输出分为2行,第一行为兄弟个数,第二行输出指定第几个兄弟,要是没有就不输出。
//2.找出的兄弟要以字典序排列
#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int const maxNum = 1000 + 1;

bool equSring(string a,string b);  //b是不是就是a
bool isBrother(string a,string b); //b是不是a的兄弟
bool cmp(string a ,string b){      //字典序排列
	return a<b;  
}
int main(){
	int n;
	while(cin>>n){
		string inp1[maxNum];
		for(int i=0;i<n;i++) cin>>inp1[i];
		string lookAns;
		int posAns;
		cin>>lookAns>>posAns;
		vector<string> aa;
		for(int i=0;i<n;i++){
			if(isBrother(lookAns,inp1[i])){
				aa.push_back(inp1[i]);
			}
		}
		sort(aa.begin(),aa.end(),cmp);
		if(posAns<=aa.size()){
			cout<<aa.size()<<endl;
			cout<<aa[posAns-1]<<endl;
		}else
			cout<<aa.size()<<endl;
	}
	return 0;
}
bool equSring(string a,string b){
	return a.compare(b);
}
bool isBrother(string a,string b){ //b是不是a的兄弟

	if((a.length() ==b.length())&&(equSring(a,b))){
		int len = b.length();
		char* p = new char[len];
		for(int i=0;i<len;i++) p[i]=b[i];
		sort(p,p+len);
		char* pp = new char[len];
		for(int i=0;i<len;i++) pp[i]=a[i];
		sort(pp,pp+len);
		for(int i=0;i<len;i++){
			if(p[i]!=pp[i]){
				delete p;
				delete pp;
				return false;
			}   
		}
		delete p;
		delete pp;
		return true;

	}else
		return false;
}

发表于 2017-05-31 12:09:37 回复(2)
#这题够坑,后面输出判定,调试了很久。。。。
while True:
    try:
        ls=list(raw_input().split())
        s=ls[-2]
        lm=ls[1:-2]
        ll=[]
        count=0
        for i in lm:
            if s!=i:
                m=list(s)
                m.sort()
                n=list(i)
                n.sort()
                if "".join(m)=="".join(n):
                    count+=1
                    ll.append(i)
        ll.sort()
        if count!=0 and int(ls[-1])<=len(ll) and ll:
            print(count)
            print(ll[int(ls[-1])-1])
        elif count!=0 and int(ls[-1])>len(ll):
            print(count)
        elif count==0:
            print(count)
    except:
        break

发表于 2019-07-09 21:38:22 回复(0)
#include 
#include 
#include 
#include 
using namespace std;
bool bro_strs(string initial_str, string str_in) {
    if(initial_str.length() != str_in.length() || initial_str == str_in)
        return false;
    for(int i = 0; i < initial_str.length(); i++) {
        if(str_in.find(initial_str[i], 0) == string :: npos) {
            return false;
        } else {
            str_in.erase(str_in.find(initial_str[i], 0), 1);
        }        
    }
    return true;
}
int main() {
    int num_s; 
    while(cin >> num_s) {
        string *str_in = new string [num_s];
        for(int i = 0; i < num_s; i ++) {
            cin >> str_in[i];
        }
        string str_find; cin >> str_find;
        int find_temps; cin >> find_temps;
        vector  str_outs;
        for(int i = 0; i < num_s; i ++) {
            if(bro_strs(str_find, str_in[i])) {
                str_outs.push_back(str_in[i]);
            }
        }
        sort(str_outs.begin(), str_outs.end());
        if(!str_outs.empty())
            cout << str_outs.size() << endl;
        else
            cout << 0 << endl;
        if(find_temps <= str_outs.size())
            cout << str_outs[find_temps - 1] << endl;
    }   
    system("pause");
    return 0;
}
//华为的题目很坑 不说清楚
编辑于 2018-10-10 19:11:00 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            List<String> list = new LinkedList<String>();
            List<String> findlist = new LinkedList<String>();
            for(int i=0; i<n; i++) {
                list.add(sc.next());
            }
            String find = sc.next();
            int index = sc.nextInt();
            Collections.sort(list);
            int cnt = 0;
            for(int j=0; j<list.size(); j++) {
                String jStr = list.get(j);
                boolean flag = true;
                if(jStr.length() == find.length()) {
                    if(!jStr.equals(find)) {
                        char[] chars = find.toCharArray();
                        for(int k=0; k<chars.length; k++) {
                            if(jStr.contains(String.valueOf(chars[k]))) {
                                jStr = jStr.replaceFirst(String.valueOf(chars[k]),"");
                            } else {
                                flag = false;
                            }
                        }
                    } else {
                        flag = false;
                    }
                    if(flag) {
                        findlist.add(list.get(j));
                        cnt++;
                    }
                }
            }
            System.out.println(cnt);
            if(findlist.size() >= index)
                System.out.println(findlist.get(index-1));
        }
    }
}

发表于 2018-10-03 18:20:05 回复(3)

暴力输出,sort(String str)函数,把单词中的字母按照字典序排列并返回string
isfriend(Sring s1,String s2)通过长度一样但单词不等,sort之后的字符串一样,为判断条件
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    /**
     * 输入描述:先输入字典中单词的个数n,再输入n个单词作为字典单词。再输入一个单词,查找其在字典中兄弟单词的个数m再输入数字k
     * 输出描述:根据输入,输出查找到的兄弟单词的个数m 然后输出查找到的兄弟单词的第k个单词。
     * 而且,数字和字符串各自占一行,还要判断边界条件,当没有指定序号的兄弟时,不输出字符串
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            String[] str=new String[n];
            String result="";
            for(int i=0;i<n;i++){
                str[i]=sc.next();
            }
            Arrays.sort(str);
            String target=sc.next();
            int k=sc.nextInt();
            int count=0;//计数多少兄弟单词
           
            for(int i=0;i<n;i++){
                
                if(isfriend(target,str[i])){
                    count=count+1;
                    if(k==count){
                        result=str[i];
                    }
                }
            }
            System.out.println(count+" ");
            if(count>=k){
                System.out.println(result);
            }
            
            
        }

    }
    static String sort(String str){
        char[] s=str.toCharArray();
        String result="";
        Arrays.sort(s);
        for(int i=0;i<str.length();i++){
            result=result+s[i];
            
        }
        return result;
    }
    static boolean isfriend(String s1,String s2){
        boolean b=false;
        if(s1.length()!=s2.length()){
            b=false;
        }
        if(s1.equals(s2)){
            b=false;
        }else{
            if(sort(s1).equals(sort(s2))){
                b=true;
            }
            
        }
        return b;
    }

}
	

发表于 2018-07-13 13:25:00 回复(0)
/*测试输出正确,就是提示没通过,一直想不明白*/
#include <stdio.h>
#include <string.h>
void sort(char *str)
{
    char temp;
    for(int i=0;str[i]!='\0';i++)
    {
        for(int j=0;j<strlen(str)-1-i;j++)
        {
            if(str[j]>str[j+1])
            {
                temp=str[j];
                str[j]=str[j+1];
                str[j+1]=temp;
            }
        }
    }
}
int isbro(char *s1,char *s2)
{
    char temp1[51];
    char temp2[51];
    if(strlen(s1)==strlen(s2))
    {
        if(strcmp(s1,s2)!=0)
        {
            strcpy(temp1,s1);
            strcpy(temp2,s2);
            sort(temp1);
            sort(temp2);
            if(strcmp(temp1,temp2)==0)
                return 1;
        }
    }
    return 0;
}

int main(void)
{
    char dir[1000][51];
    char bro[1000][51];
    int num;
    char str[50];
    int count;
    scanf("%d",&num);
    for(int i=0;i<num;i++)
        scanf("%s",dir[i]);
    scanf("%s",str);
    scanf("%d",&count);
    int n=0;
    for(int i=0;i<num;i++)
    {
        if(isbro(dir[i],str))
        {
            strcpy(bro[n],dir[i]);
            n++;
        }     
    }
    /*兄弟单词排序*/
    int min;
    char temp[51];
    for(int i=0;i<n-1;i++)
    {
        min=i;
        for(int j=i+1;j<n;j++)
        {
            if(strcmp(bro[j],bro[min])<0)
                min=j;
        }
        strcpy(temp,bro[i]);
        strcpy(bro[i],bro[min]);
        strcpy(bro[min],temp);
    }
    printf("%d ",n);
    printf("%s\n",bro[count-1]);
    
    return 0;
}
/*
测试结果
测试用例:
473 bb daccb caddd bddc c baa adb ad abbcb cdaa abab a abcc ddcbd cadcc cdacd aaa a b acccd bbb dacc cc acbdd bcbb ba bacaa adda acd aaad d ab acac bc dabab abcd aacba aba daa bb ad cddab a bbaa aacad cdac acbcc cada bacd adcad cdadc bcbcc aa b acd cbaac ddcd ccb dac a dac adbcb bcda dda a ab ca dd d abd a dbb ccabd bdddd abd adc aaa baccb ccdcd a da c cadc dcdbd d aa bb a cac c ad adb ca cdcc cadd dddca d cba cb caab caa dd cd bca abc cdaa cdb bad dad bda d ddbc dab baaa adaac b a dbccd bd b bdad cdacd baa ac ddcad d bb acc aa cd cbdbb bbbcb a cc aacc c aadc dbcd a bca dd abbb ccdcd ccd ab d a a dadcd dbd bcaa c cda b ddab caaaa cdcdb b acbc ccaa dabca dcd b ba dbcc da bdbcc ab abaca bb cddc caca da dadba accdd bdac dbcd bcbbd ab bd ccb ddaa aa b b d bddd cabac aaba ab ccdb db abbd ada bdadb c abba dd cdb bca cccda badba abaa ac aabad db ccbad bddd ada dba acba b bc dd bbbbd cc cbdd cd abcaa bb ddaca acadb bbbb bddcc bdada aaa bcbda c aaa aadd cdddc adb cdbab c cbca bb aacab acdb bbdab b acbda cbdcd bda bacdc db d adcbd bccc c aaa cdd bdcd bac a aaab ddbb cd ccdbb addcc cdc c ca baadc addba dbdbd dba bbdda bcb c cdad aacac dcada cb aaad a cccab caca aad bbb dd b babbb cba bdaca db bacd bc bcbda cdda bcccd bdcda bdbcd adb cbcb a c bacba abbb adab adab b b d bca badbc baa cdb b abc aabb b d c cdab cacda d cdcda adcdc bcc bbccd b adb caba cbaaa aadb dcc add bcac bacbd bb a b c cabaa c caad c aa bcc ccab ddc dadca cdcba aaba dabbd dcb a bddb bb a c c cbc ccd dd a cabbb b caadb cb dca cbb ddaa bcadc dab a bbda cd bc ccad bbd ab c acddd cdd dbbbb daaab abbb cabc add ca caa bbbb dcab daaaa baca dcd ccacb ba bddaa acac dbcc bcc cbbcc b abba daa dbab bcca ba aa d dcdc d dcaa cbcda bd b ccbcb baa dcacd d c cbda baba d abb c cbdc da dbbb cd aabc dadc b a ddb c ddd ccdc ccd cba dbaac dcccd ddbac dbbdd bad bcdd cb dac dccd d a acdd d c cb b bcbb c a aca bcba d d bbdbc d c dabad ccca dc adb ddb dcdd dba ad ddaba c ac ddac bbbd cd a dacbb 1 
对应输出应该为:
6 adbcb 
你的输出为:
6 adbcb
*/
发表于 2018-06-14 22:20:41 回复(1)

通过总结:
(1)输出的两个结果必须换行;
(2)当需要输出的兄弟单词的序号大于存在的兄弟单词的数量时(包括了没有兄弟单词的情况)只输出数量即可(包括了0的情况)。
下面是C++泛型算法的一种实现方式:

/*
* 华为机试在线训练——2
*/
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    string words, origWord, memoryWord;
    int wordsAmount{ 0 };
    while (cin >> wordsAmount) {    //输入单词数量
        vector<string> dictionary, deal;
        int outputAmount{ 0 }, brotherWordsAmount{ 0 };
        for (int cnt = 0; cnt < wordsAmount; ++cnt) {    //循环输入每个单词
            cin >> words;    
            dictionary.push_back(words);
        }
        cin >> origWord;    //输入基准单词
        memoryWord = origWord;
        sort(origWord.begin(), origWord.end());    //error:提前排序对后序产生影响!
        cin >> outputAmount;    //输入应该输出的数量

        for (int cnt = 0; cnt < dictionary.size(); ++cnt) {    //逐个单词判断是否是兄弟单词
            if (memoryWord != dictionary[cnt]) {
                string copy = dictionary[cnt];
                sort(copy.begin(), copy.end());
                if (copy == origWord) {
                    ++brotherWordsAmount;
                    deal.push_back(dictionary[cnt]);
                }
            }
        }

        sort(deal.begin(), deal.end());    //对字典排序

        cout << brotherWordsAmount << endl;    //输出
        if (brotherWordsAmount >= outputAmount) {
            cout << deal[outputAmount - 1] << endl;
        }
    }

}
发表于 2018-02-24 09:19:13 回复(1)
#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool isBrother(string s1,string s2)
{
    if(s1==s2)//string重载了全部的六个关系运算符可直接进行比较不用strcmp
    {
        return false;
    }
    sort(s1.begin(),s1.end());
    sort(s2.begin(),s2.end());
    if(s1==s2)
    {return true;}
    else return false;
}
int main()
{
    int n;
    while(cin>>n)
    {
        vector<string> v(n);//初始化n个vector向量
        for(int i=0;i<n;i++)
            cin>>v[i];
        vector<string>b;
        int m;
        string str;
        cin>>str;
        cin>>m;
        for(int i=0;i<n;i++)
        {
            if(isBrother(str,v[i]))
            {
                b.push_back(v[i]);
            }
        }
        sort(b.begin(),b.end());
        cout<<b.size()<<endl;
        if(m<=b.size())
        {
            cout<<b[m-1]<<endl;
        }
    }
    return 0;
}

发表于 2018-01-29 19:05:51 回复(0)
上面说的问题1.边界问题,2,。换行输出问题。排序问题在题目中有(字典)
题目本身是要实现字典类功能,看到的大多数回答并不是,下面给出字典类代码。
#include<iostream>
#include<vector>
#include <map>
#include<algorithm>
#include<string>
using namespace std;
class word
{
public:
	string s;
	map<char,int>word_sp;
	void get_map();			//得到单词的字母映射
};
class dictionary
{
public:
	vector<word>wd;	
	void paixu();		//字典单词排序功能
	int find_broth_word(word x,int k,dictionary D,string &task);//找兄弟单词功能
};
bool mycomp(word a,word b)		//排序函数需要的compile参数
{
	return a.s<b.s;
}
void word::get_map()
{
	for (int i = 0; i < s.length(); i++)
	{
		++word_sp[s[i]];
	}
}
void dictionary::paixu()
{
	sort(wd.begin(),wd.end(),mycomp);
}
int dictionary::find_broth_word(word x,int k,dictionary D,string &task)//(x-要找的单词,k指定的位置,D字典,task传出K指定的兄弟单词)
{
	int n=D.wd.size(),m=0;
	vector<int>flag;
	for (int i = 0; i < n; i++)
	{
		if (x.word_sp==D.wd[i].word_sp&&x.s!=D.wd[i].s)
		{
			flag.push_back(i);	
			m++;
		}
	}
	if (k>flag.size())
	{
		cout<<flag.size()<<endl;
		m=0;
		task="";
	}
	else
	{
		task=D.wd[flag[k-1]].s;
	}
	return m;
}
int main()
{
	int n;
	while (cin>>n)
	{
		dictionary D;
		int k=0,m=0;
		string str;
		for (int i = 0; i < n; i++)	//输入字典
		{
			word w;
			cin>>w.s;
			w.get_map();
			D.wd.push_back(w);
		}
		D.paixu();		//字典排序
		word x;
		cin>>x.s>>k;	//查找单词
		x.get_map();	
		string task;
		m=D.find_broth_word(x,k,D,task);//(x-要找的单词,k指定的位置,D字典,task传出K指定的兄弟单词)
		if (task!="")
		{
			cout<<m<<endl;
			cout<<task<<endl;
		}	
	}
	return 0;
}

发表于 2017-09-14 20:56:09 回复(0)
使用算法库is_permutation函数判断是否为兄弟单词
#include <iostream>
#include <string>
#include <set>
#include <algorithm>

using namespace std;

int main(){
    string word;
    int n;

    while(cin >> n){
        multiset<string> sset;
        while(n--){
            cin >> word;
            sset.insert(word);
        }
        string serch_word;
        cin >> serch_word;
        int serch_idx;
        cin >> serch_idx;

        int cnt = 0;
        string ret;
        for(auto &s : sset){
            if(s.size() != serch_word.size())
                continue;
            if(s == serch_word)
                continue;
            if(is_permutation(s.begin(), s.end(), serch_word.begin())){
                ++cnt;
                if(cnt == serch_idx)
                    ret = s;
            }
        }
        cout << cnt << endl; // 数字与字符串各占一行
        if(cnt != 0 && serch_idx <= cnt){ // 判断边界条件, 防止多输出换行
            cout << ret << endl;
        }
    }
    return 0;
}

发表于 2017-08-23 17:10:56 回复(0)
package brother_words;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int words_num = scanner.nextInt();
			String[] dic_words = new String[words_num];
			for (int i = 0; i < words_num; i++) {
				dic_words[i] = scanner.next();
			}
			String target_words = scanner.next();
			int brothers_index = scanner.nextInt();
			brother_word(dic_words, target_words, brothers_index);
		}
		scanner.close();

	}

	public static void brother_word(String[] dic, String target, int index) {
		Arrays.sort(dic);// 按字典顺序排序
		int bro_nums = 0;// 兄弟单词个数
		ArrayList<String> list = new ArrayList<String>();// 存放兄弟单词
		char[] tar_arr = target.toCharArray();
		Arrays.sort(tar_arr);// 排序
		for (String str : dic) {
			char[] str_arr = str.toCharArray();
			Arrays.sort(str_arr);
			if (Arrays.equals(tar_arr, str_arr) && (!str.equals(target))) {
				bro_nums++;
				list.add(str);
			}

		}

		System.out.println(bro_nums);
                 // 判断条件
		if (bro_nums != 0&&(index<=bro_nums)) {
			System.out.println(list.get(index - 1));
		}

	}

}

发表于 2017-07-29 22:46:19 回复(0)