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

输入描述:

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



输出描述:

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

示例1

输入

3 abc bca cab abc 1

输出

2
bca

尼玛,出题的语文水平简直服了。一道破题,坑比要求还多。
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 回复(5)
这个题目不难。是问题的描述让人很是糊涂。
正确的描述应该是这样的:
输入描述:
先输入字典中单词的个数n,再输入n个单词作为字典单词。
再输入一个单词,查找其在字典中兄弟单词的个数m
再输入数字k

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

编辑于 2016-08-30 20:48:33 回复(15)
//调试了很久,通过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 回复(11)
谁能告诉我这是什么情况
输出跟要求一样 说我不对



测试用例:
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 回复(15)
想了好久没做对,于是惹不住请大神出手,大神几分钟搞定,智商碾压,热腾腾的答案来了
#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.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int wordsNum = scanner.nextInt();
            String[] dict = new String[wordsNum];
            for (int i = 0; i < wordsNum; i++) {
                dict[i] = scanner.next();
            }
            String target = scanner.next();
            int brotherIndex = scanner.nextInt();
            process(dict, target, brotherIndex);
            scanner.nextLine();
        }
    }
    
    public static void process(String[] dict, String target, int brotherIndex) {
        ArrayList<String> list = new ArrayList<>();
        Arrays.sort(dict);
        for (String cur : dict) {
            if (isBrother(target, cur)) {
                list.add(cur);
            }
        }
        System.out.println(list.size());
        if (brotherIndex <= list.size() && list.size() > 0) {
            System.out.println(list.get(brotherIndex - 1));
        }
    }
    
    public static boolean isBrother(String target, String cur) {
        // target can not same as cur
        if (target.equals(cur)) {
            return false;
        }
        // length must be same
        if (target.length() != cur.length()) {
            return false;
        }
        // norm case
        char[] targetArray = target.toCharArray();
        char[] curArray = cur.toCharArray();
        // sortted array must be same
        Arrays.sort(targetArray);
        Arrays.sort(curArray);
        for (int i = 0; i < curArray.length; i++) {
            if (targetArray[i] != curArray[i]) {
                return false;
            }
        }
        return true;
    }
}

发表于 2019-08-27 16:34:20 回复(2)
这是个什么玩意?明明是对的,说不对

测试用例:
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)
哪位大神帮我看看为啥总是报数组越界呀
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)
#这题够坑,后面输出判定,调试了很久。。。。
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)
//朋友们,这道题有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)
这道题感觉有点弱智
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
    int n ;

    while(cin>>n)
    {
        string s[1002];
        string s2[1002];
        for(int i=1; i<=n; i++)
        {
            cin>>s[i];
        }
        string ms;
        cin>>ms;
        int mnum;
        cin>>mnum;
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            if(s[i].length()==ms.length()&&s[i]!=ms)
            {
                int flag=0;
                for(int j=0; j<s[i].length(); j++)
                {
                    if(count(s[i].begin(),s[i].end(),ms[j])!=count(ms.begin(),ms.end(),ms[j]))
                    {
                        flag=1;
                        break;

                    }

                }
                if(flag==0)
                {
                    sum++;
                    s2[sum]=s[i];
                }

            }
        }

        for(int i=1; i<=sum; i++)
            for(int j=1; j<=sum-i; j++)
            {
                //cout<<s2[i]<<endl;
                if(s2[j]>s2[j+1])
                {
                    string temp=s2[j];
                    s2[j]=s2[j+1];
                    s2[j+1]=temp;
                }

            }
           //  for(int i=1; i<=sum; i++)
             //   cout<<s2[i]<<endl;

       // cout<<"-----------------------------------------------"<<endl;

        if(mnum<=sum)
        {
            if(sum!=0)
            {
                cout<<sum<<endl;
                cout<<s2[mnum]<<endl;
            }
            else
                cout<<sum<<endl;
        }
        else
            cout<<sum<<endl;
    }
    return 0;
}


发表于 2019-10-04 15:50:34 回复(0)
//那个大佬帮忙看一下,我这个输出为什么是这样的
//对应输出应该为: //6 adbcb  //你的输出为: //5 adbcb
import java.util.*;
public class Main{
    public static ArrayList<String> list1 = new ArrayList<>();
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            ArrayList<String> list = new ArrayList<>();
            for(int i = 0;i<n;i++){
                String a = in.next();
                list.add(a);
            }
            String str = in.next();
            int m = in.nextInt();
            list1.add(str);
            char[] ar = str.toCharArray();
            dir(list,ar,0);
            String s = list1.get(m);
            int k = list1.size();
            System.out.println(k-1);
            System.out.println(s);
            list.clear();
            list1.clear();
        }
    }
    private static void dir(ArrayList<String> list,char[] ar,int i){
        if(list.contains(String.valueOf(ar))){
            if(!list1.contains(String.valueOf(ar)))
                list1.add(String.valueOf(ar));
        }
        for(int j = i;j<ar.length;j++){
            swap(ar,i,j);
            dir(list,ar,i+1);
            swap(ar,i,j);
        }
    }
    private static void swap(char[] ch,int i,int j){
        char tmp = ch[i];
        ch[i] = ch[j];
        ch[j] = tmp;
    }
}

编辑于 2019-08-21 09:00:43 回复(0)
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
/*
    输入单次个数、单词、要查找单词、兄弟单词位置
    兄弟单词个数、第几个兄弟单词
    注意:总个数必须输出;第几个兄弟单词超出时不要输出;个数与单词均为换行间隔
*/

bool isbro(string s1, string s2)
{
    if(s1!=s2 && s1.size()==s2.size()) {
        sort(s1.begin(), s1.end());
        sort(s2.begin(), s2.end());
        if(s1==s2)
            return true;
    }
    
     return false;
}

int main()
{
    string str;
    int n;
    
    while(cin>>n){
        vector<string> strs, sbro;
        string sstr;
        int k;
        
        for(int i=0; i!=n ;i++) {
            cin>>str;
            strs.push_back(str);
        }
        
        cin>>sstr;
        cin>>k;
        
        sort(strs.begin(), strs.end());
        
        for(int i=0; i!=strs.size(); i++) {
            if(isbro(strs[i], sstr)) {
                sbro.push_back(strs[i]);
            }
        }
        
        cout<<sbro.size()<<endl;
        if(k<=sbro.size())
            cout<<sbro[k-1]<<endl;
    }
}
发表于 2019-08-09 08:37:27 回复(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)