首页 > 试题广场 >

查找兄弟单词

[编程题]查找兄弟单词
  • 热度指数:430689 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}定义一个由小写字母构成的字符串 s 的“兄弟单词”为:任意交换 s 中两个字母的位置,得到的新字符串,且其与 s 不同。
\hspace{15pt}现在,对于给定的 n 个字符串 s_1, s_2, \dots, s_n 和另一个单独的字符串 x ,你需要解决两个问题:
\hspace{23pt}\bullet\,统计这 n 个字符串中,有多少个是 x 的“兄弟单词”;
\hspace{23pt}\bullet\,将这 n 个字符串中 x 的“兄弟单词”按字典序从小到大排序,输出排序后的第 k 个兄弟单词。特别地,如果不存在,则不输出任何内容。

\hspace{15pt}从字符串的第一个字符开始逐个比较,直到找到第一个不同的位置,通过比较这个位置字符的字母表顺序得出字符串的大小,称为字典序比较。

输入描述:
\hspace{15pt}在一行上:
{\hspace{20pt}}_\texttt{1.}\,先输入一个整数 n \left(1 \leqq n \leqq 1000\right) 代表字符串的个数;
{\hspace{20pt}}_\texttt{2.}\,随后,输入 n 个长度为 1 \leqq {\rm length}(s_i) \leqq 10 ,仅由小写字母构成的字符串 s_1, s_2, \dots, s_n
{\hspace{20pt}}_\texttt{3.}\,随后,输入一个字符串 x
{\hspace{20pt}}_\texttt{4.}\,最后,输入一个整数 k \left(1 \leqq k \leqq n\right) 代表要查找的兄弟单词的序号。


输出描述:
\hspace{15pt}第一行输出一个整数,代表给定的 n 个字符串中,x 的“兄弟单词”的数量;
\hspace{15pt}第二行输出一个字符串,代表将给定的 n 个字符串中 x 的“兄弟单词”按字典序排序后的第 k 小兄弟单词。特别地,如果不存在,则不输出任何内容。
示例1

输入

3 abc bca cab abc 1

输出

2
bca

说明

\hspace{15pt}在这个样例中,x 的兄弟单词为 \texttt{\texttt{\texttt{\texttt{\texttt{ 。其中,标橙色的两个字符串存在于所给定的 n 个字符串中。
示例2

输入

3 a aa aaa a 1

输出

0

说明

\hspace{15pt}在这个样例中,按照定义,字符串 \texttt{ 没有兄弟单词。
大佬们,有一个例子通不过,能帮忙看看代码吗?
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
    int n;
    scanf("%d",&n);
    int i,j,l,flag=1,index=0;
    char str[n][20];
    for(i=0;i<n;i++)
    scanf("%s",&str[i]);
    char x[20];
    int k;
    scanf("%s",&x);
    scanf("%d",&k);

    int countx[26],counts[26];
    memset(countx, 0, sizeof(countx));
    memset(counts, 0, sizeof(counts));
    char d[n][20];
    for(i=0;i<strlen(x);i++)
    {
        countx[x[i]-'a']++;
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<strlen(str[i]);j++)
        {
            counts[str[i][j]-'a']++;
        }
        for(l=0;l<26;l++)
        {
            if(countx[l]!=counts[l])
            flag=0;
        }
        if(flag==1&&strcmp(x,str[i])!=0)strcpy(d[index++],str[i]);
        else flag=1;
        memset(counts, 0, sizeof(counts));
    }
    printf("%d\n",index);
    for(i=0;i<index;i++)
    {
        for(j=i;j<index;j++)
        {
            if(strcmp(str[i],str[j])>0)
            {
                char temp[20];
                strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],temp);
            }

        }

    }
    if(k<=index)
    printf("%s\n",d[k-1]);

}


发表于 2025-03-11 15:01:14 回复(1)
#include <stdio.h>

typedef struct str {
    char s[11];
} Str;

int com(const void * a, const void * b){
    char* parA = ((Str*)a)->s;
    char* parB = ((Str*)b)->s;

    for (int i = 0; i < strlen(parA); i ++){
        if (parA[i] != parB[i])
            return parA[i] - parB[i];
    }

    return 0;
}

int is_brother(char str[], char x[]) {
    if (strlen(str) != strlen(x) || strcmp(str, x) == 0)
        return 0;

    // 字典哈希表
    int ziDian[26];
    memset(ziDian, 0, sizeof(int) * 26);

    for (int j = 0; j < strlen(x); j ++) {
        ziDian[x[j] - 'a']++;
    }

    for (int j = 0; j < strlen(str); j ++) {
        ziDian[str[j] - 'a']--;
    }

    for (int j = 0; j < 26; j ++) {
        if (ziDian[j] != 0)
            return 0;
    }

    return 1;
}

int find_word(Str arr[], char x[], int len) {
    int res = 0;

    for (int i = 0; i < len; i ++) {
        if (is_brother(arr[i].s, x))    res ++;
    }

    return res;
}

int main() {
    int n;
    scanf("%d", &n);
    Str arr[n];
    memset(arr, 0, sizeof(Str) * n);
    for (int i = 0; i < n; i ++)
        scanf("%s", arr[i].s);

    char x[11];
    memset(x, 0, sizeof(char) * 11);
    scanf("%s", x);
    int k;
    scanf("%d", &k);

    // 找出有多少个兄弟单词
    int num = find_word(arr, x, n);
    printf("%d\n", num);

    Str brother[num];
    memset(brother, 0, sizeof(Str) * num);
    int index = 0;
    for (int i = 0; i < n; i ++){
        if (is_brother(arr[i].s, x)){
            memcpy(&brother[index], &arr[i], sizeof(Str));
            index++;
        }
    }

    qsort(brother, num, sizeof(Str), com);

    if (k <= num)
        printf("%s\n", brother[k - 1].s);

    return 0;
}
发表于 2025-02-07 15:09:34 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct brotherstr {
    char str[11];
} typBrhtStr;
 
int inc(const void* a, const void* b)
{
    return strcmp((*(typBrhtStr *)a).str, (*(typBrhtStr *)b).str);
}

int isBrotherStr(typBrhtStr *a,typBrhtStr*b)
{
    int achar[26];
    int bchar[26];
    int i;
    char *p;

    /*初始化为0,非常重要*/
    memset(achar,0,sizeof(int)*26);
    memset(bchar,0,sizeof(int)*26);    

    // if(strcmp((*a).str,(*b).str))
    //     return 0;
    if(strcmp((*a).str,(*b).str)==0)
        return 0;
    else
    {
        p=(*a).str;
        while(*p!='\0')
        {
            (achar[*p-'a'])++;
            p++;
        }

        p=(*b).str;
        while(*p!='\0')
        {
            (bchar[*p-'a'])++;
            p++;
        }

        for(i=0;i<26;i++)
        {
            if(achar[i] != bchar[i])
                return 0;
        }

        return 1;
    }
}

int main()
{
    char tempstr[11];
    int i=0,nums,numsk,numbrth=0;
    typBrhtStr xBrthstr;

    while(scanf("%d",&nums)!=EOF)
    {
        typBrhtStr userBrthstr[nums];
        typBrhtStr resultBrthstr[nums];
        for(i=0;i<nums;i++)
        {
            scanf("%s",userBrthstr[i].str);
        }
        scanf("%s",xBrthstr.str);
        scanf("%d",&numsk);
        for(i=0;i<nums;i++)
        {
            if(isBrotherStr(&userBrthstr[i],&xBrthstr))
            {
                resultBrthstr[numbrth++]=userBrthstr[i];
            }
        }
        printf("%d\r\n",numbrth);

        if(numbrth)
            qsort(resultBrthstr,numbrth,sizeof(typBrhtStr),inc);

        if(numsk<numbrth)
            //printf("%s",resultBrthstr[i].str);
            //printf("%s",resultBrthstr[numsk].str);
            printf("%s",resultBrthstr[numsk-1].str);
        return 0;

    }
    return 0;
}
发表于 2024-12-07 20:57:14 回复(0)
int main() {
	int n;
	scanf("%d", &n);
	int i;
	char s[1000][12];
	for (i = 0; i<n; i++)
	{
		scanf("%s", s[i]);
	}
	char key[12];
	scanf("%s", key);
	int k;
	scanf("%d", &k);


	int keyLen = strlen(key);
	int keyhash[128] = { 0 };
	int keyNumIndex[10];   // 记录 hash表的索引
	float Key_Zidian = 0;   // 字典

	int KeyhashNum = 0;   // 字符种类
	for (i = 0; i<keyLen; i++)
	{
		if (keyhash[key[i]] == 0)  // 没有 表示是新的种类
		{
			keyNumIndex[KeyhashNum++] = key[i];
		}
		keyhash[key[i]]++;
		Key_Zidian = Key_Zidian * 26 + key[i] - 'a';
	}


	int bro_index[10];   // 兄弟字母的索引
	float bor_zidian[10];
	int bor_num = 0;
	int j;
	for (i = 0; i<n; i++)
	{
		int tempHash[128] = { 0 };
		float tempZidian = 0;
		int kind = 0; // 记录种类
		int tempLen = strlen(s[i]);
		if (tempLen != keyLen)
			continue;

		for (j = 0; j<tempLen; j++)
		{
			if (tempHash[s[i][j]] == 0)  // 没有 表示是新的种类
			{
				kind++;
			}
			tempHash[s[i][j]]++;
			tempZidian = tempZidian * 26 + s[i][j] - 'a';
		}

		// 判断是不是兄弟
		if (kind == KeyhashNum && tempZidian != Key_Zidian) // 种类相同,但是两个不是重复
		{
			// 判断数量是否相同
			for (j = 0; j<KeyhashNum; j++)
			{
				if (keyhash[keyNumIndex[j]] != tempHash[keyNumIndex[j]]) // 判断数量
				{
					break;
				}
			}

			if (j == KeyhashNum)  // 说明是兄弟
			{
				bor_zidian[bor_num] = tempZidian;
				bro_index[bor_num++] = i; // 记录第i个字符串的索引
			}
		}

	}


	// 冒泡排序
	for (j = 0; j<bor_num; j++)
	{
		for (i = 0; i<bor_num - j - 1; i++)
		{
			if (bor_zidian[i]>bor_zidian[i + 1])
			{
				float temp = bor_zidian[i];
				bor_zidian[i] = bor_zidian[i + 1];
				bor_zidian[i + 1] = temp;

				int indexTep = bro_index[i];
				bro_index[i] = bro_index[i + 1];
				bro_index[i + 1] = indexTep;
			}
		}
	}



	// for (i = 0; i<bor_num; i++)
	// 	printf("%s\n", s[bro_index[i]]);

    printf("%d\n",bor_num);
    if(k<=bor_num)  // 增加约束
        printf("%s", s[bro_index[k-1]]);	
    return 0;
}

发表于 2024-08-07 10:46:52 回复(0)
#include <stdio.h>
#include <string.h>

int isBro(char* a, char* b) {
    int i;
    int count[26];

    if ( strlen(a) != strlen(b) )
        return 0;
    if ( strcmp(a, b) == 0)
        return 0;

    for ( i = 0; i < 26; i++) { //简单哈希表,记录单词字母出现的次数
        count[i] = 0;
    }

    for ( i = 0; i < strlen(a); i++) {
        count[a[i] - 'a']++;
    }

    for ( i = 0; i < strlen(b); i++) {
        count[b[i] - 'a']--;
    }

    for ( i = 0; i < 26; i++) {
        if ( count[i] != 0 ) //兄弟单词两项相消,count应全为0
            return 0;
    }

    return 1;
}

int main() {
    int n, i, j;
    char temp[11];
    char str[1000][11];
    char bro[1000][11];
    char key[11];
    int num = 0, k;

    while (scanf("%d", &n) != EOF) { // 注意 while 处理多个 case
        for (i = 0; i < n; i++) {
            scanf("%s", str[i]);
        }
        scanf("%s", key);
        scanf("%d", &k);

        //先筛选兄弟单词
        for (i = 0; i < n; i++) {
            if (isBro(str[i], key)) {              
                strcpy(bro[num],str[i]);
                num++;
            }
        }

        //将兄弟单词字典排序
        for (i = 0; i < num; i++) {
            for (j = i + 1; j < num; j++) {
                if (strcmp(bro[i], bro[j]) > 0) {
                    strcpy(temp, bro[i]);
                    strcpy(bro[i], bro[j]);
                    strcpy(bro[j], temp);
                }
            }
        }

        printf("%d\n", num);

        if (k <= num) {
            printf("%s",bro[k-1]);
        }

    }
    return 0;
}




发表于 2024-06-22 11:28:23 回复(0)
能公开最后一个例子吗?实在是不知道哪里没考虑到!
编辑于 2024-04-13 00:31:19 回复(1)
字典序我还以为是原来他给的字典里的顺序呢

编辑于 2024-04-04 23:32:51 回复(0)
#include <stdio.h>
#include <string.h>
void wordsort(char str[])
{
    int len=strlen(str);
    for(int i=0;i<len-1;i++)
        for(int j=len-1;j>i;j--)
            if(str[j]<str[j-1])
            {
                char s=str[j-1];
                str[j-1]=str[j];
                str[j]=s;
            }
}
int main() {
    int n,k,count=0;
    int tag[1000]={0};/*用于记录兄弟单词所在的位置*/
    char dic[1001][11]={0},str[11]={0};
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%s",dic[i]);
    scanf("%s",str);
    scanf("%d",&k);
    char strcopy[11]={0};
    int len1=strlen(str);
    for(int i=0;i<len1;i++)
        strcopy[i]=str[i];
    wordsort(strcopy);
    for(int i=0;i<n;i++)
    {
        if(strcmp(str,dic[i])==0)
            continue;
        char dicopy[11]={0};
        strcpy(dicopy,dic[i]);
        wordsort(dicopy);
        if(strcmp(dicopy,strcopy)==0)
            tag[count++]=i;
    }
    printf("%d\n",count);
    if(count>=k)
    {
        for(int i=0;i<count-1;i++)/*对字典里的字符串排序*/
            for(int j=count-1;j>i;j--)
                if(strcmp(dic[tag[j]],dic[tag[j-1]])==-1)
                {
                    char ex[11]={0};
                    strcpy(ex,dic[tag[j-1]]);
                    strcpy(dic[tag[j-1]],dic[tag[j]]);
                    strcpy(dic[tag[j]],ex);
                }
        printf("%s",dic[tag[k-1]]);
    }
    return 0;
}

最后一个例子过不了,数据太长不知道哪里出错了
发表于 2024-03-28 20:59:36 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int comp(char *a, char *b)
{
    return strncmp(a,b,11);
}

int main()
{
    int n, i, j, k;
    char str[1000][11]={0}, x[11]={0}, bro[1000][11]={0};
    int sum[26]={0}, cnt[26]={0};
    int count=0;               //记录兄弟单词的个数

    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",&str[i]);
    }
    scanf("%s %d", &x, &k);
    for(i=0;i<strlen(x);i++)
    {
        sum[x[i]-'a']++;
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<strlen(str[i]);j++)
        {
            cnt[str[i][j]-'a']++;
        }
        int flag=1;
        for(j=0;j<26;j++)           //判断两个单词中的字母出现次数是否一样
        {
            if(sum[j]!=cnt[j])
            {
                flag=0;
                break;
            }
        }
        if(flag==1 && strcmp(str[i],x)!=0) strcpy(bro[count++],str[i]);
        for(j=0;j<26;j++)           //记录字母出现次数的cnt数组清零
        {
            cnt[j]=0;
        }
    }
    qsort(bro, count, sizeof(bro[0]), comp);     //排序
    printf("%d\n",count);
    if(k<=count) printf("%s",bro[k-1]);
    
    return 0;
}

发表于 2023-11-30 19:55:37 回复(0)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int Compare(const void *a, const void *b)
{
    return *(char*)a - *(char*)b;
}

int CompareBrother(const void *a, const void* b)
{
    return strcmp((char*)a, (char*)b);
}

bool JudgeBrother(char tar[], char string[], int tarLen, int strLen)
{
    if(strLen != tarLen){
        return false;
    }
    if(strcmp(tar, string) == 0){
        return false;
    }
    char tar1[tarLen], string1[strLen];
    for(int i=0; i<tarLen; i++){
        tar1[i] = tar[i];
        string1[i] = string[i];
    }
    qsort(tar1, tarLen, sizeof(tar[0]), Compare);
    qsort(string1, strLen, sizeof(string[0]), Compare);
    if(strcmp(tar1, string1) == 0){
        return true;
    }
    return false;
}

int main()
{
    int n, k;
    scanf("%d", &n);
    char s[n][11];
    char brother[n][11];
    char tar[11];
    int cnt = -1;
    for(int i = 0; i < n; i++){
        scanf("%s", s[i]);
    }
    scanf("%s", tar);
    scanf("%d", &k);
    for(int i = 0; i < n; i++){
        if(JudgeBrother(tar, s[i], strlen(tar), strlen(s[i]))){
            strcpy(brother[++cnt], s[i]);
        }
    }
    qsort(brother, cnt+1, sizeof(brother[0]), CompareBrother);
    printf("%d\n", cnt+1);
    if(k <= cnt+1){
        printf("%s\n", brother[k-1]);
    }
    return 0;
}

发表于 2023-10-05 15:10:03 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp_str(const void* e1,const void* e2)
{
    return strncmp(*(char**)e1,*(char**)e2,12);
}
int main() {
    char str[20000];
    char tmp [12];
    char word [12];
    char buf[26] = {0};
    int i =0;
    int n =0,k=0;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",tmp);
        sprintf(str,"%s-%s",str,tmp);
    }
    scanf("%s",word);
    int word_len = strlen(word);
	for(int i=0;i<26;i++)
		buf[i] = '0';
    for(int i =0;i<word_len;i++)
    	buf[word[i]-'a']++;

    scanf("%d",&k);
    const char* c = "-";
    char* token;
    token = strtok(str,c);
    char* ret[10002];
    int idx=0;
    while(token!=NULL)
    {
        int t_len = strlen(token);
        for(int i =0;i<t_len;i++)
        {
        	buf[token[i]-'a']--;
        }
        int flag = 0;
        for(int i=0;i<26;i++)
        {
            if(buf[i]!='0')
            {
                flag =1;
                break;
            }
        }
		for(int i=0;i<26;i++)
		buf[i] = '0';
		for(int i =0;i<word_len;i++)
            buf[word[i]-'a']++;
        if(flag || strncmp(token,word,12)==0)
        {    
       	 	token = strtok(NULL,c);
        	continue;
        }		
        char* t_str= malloc(12);
        memcpy(t_str,token,12);
        ret[idx++] = t_str;
        token = strtok(NULL,c);
    }
    qsort(ret,idx,sizeof(char*),cmp_str);
    printf("%d\n",idx);
    if(k<=idx)
        printf("%s\n",ret[k-1]);
    return 0;
}

发表于 2023-06-20 22:49:59 回复(0)
#include <stdio.h>
#include <string.h>
int hash_cmp_load(char arr[],char pload[])  //判断兄弟单词
{
    int w[26]={0},w_arr[26]={0};
    int len1,len2,i;
    len1=strlen(arr);
    len2=strlen(pload);
    for(i=0;i<len2;i++)
    {
        w[pload[i]-'a']++;
    }
    for(i=0;i<len1;i++)
    {
        w_arr[arr[i]-'a']++;
    }
    for(i=0;i<26;i++)
    {
        if(w_arr[i])
        {
            if(w_arr[i]!=w[i])
                return 0;
        }
    }
    return 1;
}
int main()
{
    int num,i,k,len,result_num=0,len_load;
    int j=0,l;
    scanf("%d",&num);
    char buf[num][10],load[10],brother[num][10];
    memset(brother,0,sizeof(brother[num][10]));
    for(i=0;i<num;i++)
    {
        scanf("%s",buf[i]);
    }
    scanf("%s",load);
    scanf("%d",&k);
    len_load=strlen(load);
    for(i=0;i<num;i++)
    {
        len=strlen(buf[i]);
        if(len==len_load&&strcmp(buf[i],load)!=0) //计算兄弟单词的数量
        {
            if(hash_cmp_load(buf[i], load))
            {
                result_num++;
                strcpy(brother[j],buf[i]);
                j++;
            }
        }
    }
    for(i=0;i<j;i++)
    {
        for(l=i+1;l<j;l++)
        {
            if(strcmp(brother[i],brother[l])>0)
            {
                strcpy(load,brother[i]);
                strcpy(brother[i],brother[l]);
                strcpy(brother[l],load);
            }
        }
    }
    printf("%d\n",result_num);
    if(k>result_num)
        return 0;
    else
        printf("%s\n",brother[k-1]);
    return 0;
}
发表于 2023-04-20 13:10:34 回复(0)
#include <stdio.h>
#include <stdlib.h>

typedef struct word{
        char data[11];
        long int  quan;
        struct word *next;
 } word;

void f(char *a , word *h , int *d , int *let ){
    int lett[26]={0};
    int i;
    long int value=0;
    for( i=0 ; a[i]!='\0' ; i++ ){
        value=value*26+a[i]-'a'+1;
        lett[a[i]-'a']++;
    }
    int flag=1;
    for( i=0 ; i<26 ;i++ ){
        if(let[i]!=lett[i]) {flag=0; break;}
    }
    if(flag){
        *d=*d+1;
        word *p=(word*)malloc(sizeof(word)),*q=h;
        for( i=0 ; a[i]!='\0' ; i++ ){
            p->data[i]=a[i];
        }
        p->data[i]='\0';
        p->quan=value;
        while (q->next!=NULL) {
            if(value < q->next->quan) {  p->next=q->next,q->next=p;             break;}
            q=q->next;
        }
        if(q->next==NULL) q->next=p,p->next=NULL;
    }
}

int main() {
    
    int n,k,i;
    scanf("%d",&n);
    char a[n][11];
    for( i=0 ; i<n ; i++ ){
        scanf("%s",a[i]);
    }
    char x[11];
    scanf("%s",x);
    scanf("%d",&k);

    int let[26]={0};        //26个英文字母
    for(i=0 ; i<strlen(x) ; i++ ){
        let[x[i]-'a']++;
    }

    word *h=(word*)malloc(sizeof(word));
    h->next=NULL,h->quan=0;
    int d=0;

    for(i=0 ; i<n ;i++ ){
        if(strcmp(a[i],x))
            f( a[i], h, &d, let );
    }

    word *p=h;
    for(i=0 ; i<k&&p!=NULL ; i++)   {p=p->next;}
    printf("%d\n",d);
    if(p) printf("%s",p->data);

    return 0;
}

发表于 2023-04-07 16:15:44 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 比较函数,用于qsort函数排序
int compare(const void* a, const void* b) {
    return strcmp(a, b);
}

int main() {
    int num, id, count = 0;
    char word[11], order[11], tmp[11];

    // 输入单词个数
    scanf("%d", &num);
    // 存储输入的单词
    char str[num][11];
    for (int i = 0; i < num; i++) 
        scanf("%s", str[i]);
    // 输入待查找的单词和位置
    scanf("%s%d", word, &id);

    // 将待查找的单词排序,方便后面比较
    strcpy(order, word);
    qsort(order, strlen(word), sizeof(char), compare);
    // 遍历所有单词,将与待查找单词字母相同的单词进行排序并比较,统计符合要求的单词数量
    for (int i = 0; i < num; i++) {
        if (!strcmp(str[i], word))  // 如果当前单词与待查找单词一致,则跳过
            continue;
        strcpy(tmp, str[i]); // 否则将当前单词复制到临时变量中
        qsort(tmp, strlen(tmp), sizeof(char), compare); // 对临时变量进行排序
        if (!strcmp(tmp, order)) // 如果排序后的单词与待查找单词排序后的结果一致,则说明这两个单词的字母组成相同
            strcpy(str[count++], str[i]); // 将当前单词添加到输出结果中
    }
    // 对符合要求的单词进行排序
    qsort(str, count, sizeof(*str), compare);

    // 输出符合要求的单词数量,并输出第id个单词(如果存在)
    printf("%d\n", count);
    if (id <= count) 
        printf("%s", str[id - 1]);
}

发表于 2023-03-22 22:52:10 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//判断是否是兄弟单词
int bro(char *a,char *b){
    int al=strlen(a);
    int bl=strlen(b);
    if(al!=bl)
        return 0;
    int fa[26]={0};
    int fb[26]={0};
    for(int i=0;i<=al;i++){
        fa[a[i]-'a']++;
        fb[b[i]-'a']++;
    }
    for(int i=0;i<26;i++){
        if(fa[i]!=fb[i])
            return 0;
    }
    if(strcmp(a,b)==0)
        return 0;
    return 1;
}

//字符串排序函数
int cmp(const void* a,const void* b){
    char* t1 = *(char**)a;
    char* t2 = *(char**)b;
    return strcmp(t1,t2);
}

int main() {
    int n,k;
    scanf("%d",&n);
    char* word[n];
    char x[10];
    for(int i=0;i<n;i++){
        word[i]=(char*)malloc(10*sizeof(char));
        scanf("%s",word[i]);
    }
    scanf("%s",x);
    scanf("%d",&k);
    int index=0;
    int num=0;

    //首先挑出给定的n个单词中的兄弟单词存放x1中
    char *x1[n];
    while(index<n){
        if(bro(word[index],x)){
            x1[num++] = word[index];
        }      
        index++;
    }

    //排字典序
    qsort(x1,num,sizeof(char *),cmp);
    printf("%d\n",num);
    if(num>=k){
        printf("%s\n",x1[k-1]);
    }
    return 0;
}
发表于 2023-03-04 19:10:41 回复(0)
#include <stdio.h>
#include <string.h>

#define SIZE 11
#define LEN 26    //单词字母数量(本来也算上大写字母的,但是所有测试用例都不涉及到大写字母,若需要大写之母则改成54)

int main() {
    int n = 0;
    scanf("%d", &n);

    char str[n][SIZE];//定义二维数组存储单词
    int slen[n];//每个单词的长度记录
    int letter_count[LEN] = {0};//哈希表记录所查询单词的每个字母出现次数
    int x_count[LEN] = {0};//哈希表记录所给出被查询的单词(即x)的每个字母出现次数
    char x[SIZE];//该数组表示题目所示的单词x
    int k = 0;
    int xlen;//单词x的长度
    int m = 0;//记录兄弟单词的个数
    int flag[n];//定义一个数组来记录x的兄弟单词在str数组中的行下标
    int s = 0;
   
   
    for (int i=0; i<n; i++) {
        scanf("%s", str[i]);
    }
    scanf("%s", x);
    xlen = strlen(x);
   
    for (int i=0; i<xlen; i++) {//只需要循环单词的长度,因为其他的没有出现的都被初始化为“0”了
        x_count[x[i]-'a']++;
    }

    scanf("%d", &k);

    for (int i=0; i<n; i++) {
        slen[i] = strlen(str[i]);
        if ((slen[i] == xlen) && (strcmp(x, str[i]))) {            
            for (int j=0; j<slen[i]; j++) {
                letter_count[str[i][j]-'a']++;
            }
            int count = 0;
            for (int t=0; t<LEN; t++) {
                /*判断两个单词的字母计数数组是否完全相同*/
                if (x_count[t] == letter_count[t]) {
                    count++;
                }
                else {//一旦出现有字母计数不同的就表明不是兄弟单词,直接退出
                    break;
                }
            }
            /*如果条件满足则表明所有的字母计数都完全一样,找到兄弟单词了*/
            if (count == LEN) {
                m++;
                flag[s++] = i;//记录原数组单词的定位下标
            }
        }
        /*这里要重新给letter_count赋初值“0”,因为要给下个单词的字母重新计数*/
        memset(letter_count, 0, sizeof(letter_count));
    }

    /*定义二维数组存储兄弟单词,当m远远小于n时,在这里定义数组可以少占用一些内存空间*/
    char sort[m][SIZE];
    for (int i=0; i<m; i++) {
        /*把找到的兄弟单词依次复制到要存储兄弟单词的数组中*/
        strcpy(sort[i], str[flag[i]]);
    }
   
   /*要对兄弟单词数组进行排序(小到大),这里使用选择排序*/
    for (int i=0; i<m-1; i++) {
        int min = i;
        for (int j=i+1; j<m; j++) {
            if (strcmp(sort[min], sort[j]) > 0) {
                min = j;//min永远表示每一轮的数组的最小值的下标
            }
        }
        //每一轮查找循环完成之后进行位置交换
        char temp[SIZE];
        strcpy(temp, sort[min]);
        strcpy(sort[min], sort[i]);
        strcpy(sort[i], temp);
    }

    printf("%d\n", m);
    if (k <= m) {//所给的k要小于兄弟单词的单词总数才可进行输出
        printf("%s\n", sort[k-1]);
    }
   
    return 0;
}
发表于 2023-03-02 12:13:44 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    int n;
    char str[1000][11] = {0};
    char brother[1000][11] = {0};
    char x[11] = {0};
    int k;
    int count[26] = {0}; //记录26个字母
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", str[i]);
    }
    scanf("%s", x);
    scanf("%d", &k);

    //查找兄弟单词x
    int p = 0;
    int brother_num = 0;
    int len1 = strlen(x);
    for (int i = 0; i < n; i++) {
        int flag = 1;
        int len2 = strlen(str[i]);
        if (len1 != len2 || strcmp(str[i], x) == 0)
            continue;       //长度不等或重复单词直接跳过
        for (int j = 0; j < len2; j++) {
            count[str[i][j] - 'a']++;  //记录单词字母出现的次数
        }
        for (int j = 0; j < len1; j++) {
            count[x[j] - 'a']--;
        }
        for (int j = 0; j < 26; j++) {
            if (count[j] != 0) {
                flag = 0;
                break;              //不是兄弟单词
            }

        }
        if (flag == 0) {
            memset(count, 0, sizeof(count));
            continue;
        }

        //记录兄弟单词
        strcpy(brother[p],  str[i]);
        p++;
        brother_num++;
        memset(count, 0, sizeof(count));
    }

    //对兄弟单词排序
    for (int i = 0; i < p - 1; i++) {
        for (int j = 0; j < p - 1 - i; j++) {
            if (strcmp(brother[j], brother[j + 1]) > 0) {
                char temp[11];
                strcpy(temp, brother[j]);
                strcpy(brother[j], brother[j + 1]);
                strcpy(brother[j + 1], temp);
            }
        }
    }

    printf("%d\n", brother_num);
    printf("%s", brother[k - 1]);
    return 0;
}

发表于 2023-02-25 17:35:38 回复(0)
以等长且字符乘积相同的条件来判断是否为兄弟单词(因为出现反例的概率极低,这里投机取巧一下)
#include<stdio.h>
#include<string.h>

struct A{
	char s[15]; 
	int num;
}st[1010],tt[1010];
 
int main(){
	int n,i,j,m=0,f;
	scanf("%d",&n);
	for(i=0;i<n+1;i++){
		scanf(" %s",&st[i].s);
		st[i].num=1;
	}
	for(i=n;i>=0;i--){
		for(j=0;j<strlen(st[i].s);j++){
			st[i].num*=st[i].s[j];		
		}
		if(i==n){
			continue;
		}else if(strlen(st[i].s)==strlen(st[n].s)&&st[i].num==st[n].num&&strcmp(st[i].s,st[n].s)!=0){
			tt[m]=st[i];
			m++;		 
		}
	}
	scanf("%d",&f);
	printf("%d\n",m);
	if(f>m){
		return 0;
	}
	for(i=0;i<m-1;i++){
		for(j=0;j<m-1-i;j++){
			if(strcmp(tt[j].s,tt[j+1].s)>0){
				strcpy(st[i].s,tt[j].s);
				strcpy(tt[j].s,tt[j+1].s);
				strcpy(tt[j+1].s,st[i].s);
			} 
		}
	}
	printf("%s\n",tt[f-1].s);
	return 0;
}


发表于 2022-08-05 21:21:54 回复(0)