【名词解释】
在一行上依次输入:
一个整数
代表字符串的个数;
个长度为
,仅由小写字母构成的字符串
;
一个长度为
,仅由小写字母构成的字符串
;
一个整数
代表要查找的第
小的兄弟单词的序号。
第一行输出一个整数,代表给定的
个字符串中,
的“兄弟单词”的数量;
第二行输出一个字符串,代表将给定的
个字符串中
的“兄弟单词”按字典序排序后的第
小兄弟单词。特别地,如果不存在,则不输出任何内容(完全省略第二行)。
3 abc bca cab abc 1
2 bca
在这个样例中,
的兄弟单词为
、
、
、
、
。其中,标橙色的两个字符串存在于所给定的
个字符串中。第
小的兄弟单词为
。
3 a aa aaa a 1
0
在这个样例中,按照定义,字符串
没有兄弟单词。
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-05-30 更新题面。
2. 2024-12-29 更新题面。
#include <stdio.h>
#include<string.h>
int son(char*x,char*y){
int a[26],b[26],i=0;
for(i=0;i<26;i++){
a[i]=0;b[i]=0;
}
for(i=0;x[i]!='\0';i++){
a[x[i]-'a']++;
b[y[i]-'a']++;
}
for(i=0;i<26;i++){
if(a[i]!=b[i])return 0;
}
return 1;//判断是否为兄弟单词
}
int main() {
int n, x;
scanf("%d",&n);
char word[n][11];
int i;
for(i=0;i<n;i++){
scanf("%s",word[i]);
}
char mod[11];
scanf("%s",mod);
scanf("%d",&x);
int sum=0,j=0;
char bro[n][11];
for(i=0;i<n;i++){
if(strlen(word[i])!=strlen(mod)){
continue;//去除长度不同的
}
if(strcmp(word[i],mod)==0)continue;//去除完全相同的
if(son(word[i],mod)==1){
strcpy(bro[j],word[i]);
sum++;
j++;//将兄弟单词单独存储
}
}
char t[11];
for(i=sum;i>0;i--){
for(j=1;j<i;j++){
if(strcmp(bro[j-1],bro[j])>0)//注意strcmp返回值
{strcpy(t,bro[j-1]);
strcpy(bro[j-1],bro[j]);
strcpy(bro[j],t);}
}//冒泡排序兄弟单词
}
if(sum==0) printf("%d",sum);//处理没有兄弟单词的情况
else printf("%d\n%s",sum,bro[x-1]);
return 0;
} 一开始一直过不去,后来知道strcmp函数不一定返回1,可能返回其他正数。于是把判断条件的=1改为>0就过了
#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]);
}
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;
} #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;
} #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;
}
#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;
} #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;
} #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]);
}
#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;
}