题解 | 查找兄弟单词
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
// #include <stdio.h>
// #include <string.h>
// #include <stdlib.h>
// #define BUFFER_SIZE (15000)
// #define STRING_SIZE (11)
// char** brother_combinations(const char* keyword) {
// int count;
// for (int i=1; i<=strlen(keyword); i++) {
// count *= i;
// }
// count --;
// char** combinations = (char**)malloc(sizeof(char*)*count);
// for (int i=0; i<strlen(keyword); i++) {
// }
// }
// int is_brother(const char* string, const char* keyword) {
// if (strlen(string) != strlen(keyword)) {
// return 0;
// }
// }
// int main() {
// char buffer[BUFFER_SIZE];
// fgets(buffer, BUFFER_SIZE, stdin); // 保存输入字符串
// if (buffer[strlen(buffer)]-1 == '\n') { // 如果有换行符去掉换行符
// buffer[strlen(buffer)-1] = '\0';
// }
// char* token = strtok(buffer, " ");
// int string_count = atoi(token); // 保存字符串个数
// // printf("%d\n", string_count);
// char** all_strings = (char**)malloc(sizeof(char*)*string_count); // 保存所有字符串
// char* keyword = NULL;
// int string_index = 0;
// while (token) { // 提取出n个字符串、字符串x和整数k
// token = strtok(NULL, " ");
// if (string_index == string_count) { // 保存字符串x和整数k
// keyword = (char*)malloc(sizeof(char)*(strlen(token)+1));
// memcpy(keyword, token, strlen(token));
// keyword[strlen(token)] = '\0';
// // printf("keyword: %s\n", keyword);
// token = strtok(NULL, " ");
// int k = atoi(token);
// // printf("k: %d\n", k);
// break;
// }
// all_strings[string_index] = (char*)malloc(sizeof(char)*(strlen(token)+1)); // 存第i个字符串
// memcpy(all_strings[string_index], token, strlen(token));
// all_strings[string_index][strlen(token)] = '\0';
// // printf("%s\n", all_strings[string_index]);
// string_index ++;
// }
// for (int i=0; i<string_count; i++) {
// if (is_brother(all_strings[i], keyword)) {
// }
// }
// // 释放内存
// for (int i=0; i<string_count; i++) {
// free(all_strings[i]);
// }
// free(all_strings);
// free(keyword);
// return 0;
// }
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 1000
#define MAXLEN 11 // 字符串长度 ≤10,再加1个'\0'
// 用于qsort比较两个字符串(字典序)
int cmpStr(const void* a, const void* b) {
const char* sa = (const char*)a;
const char* sb = (const char*)b;
return strcmp(sa, sb);
}
// 对字符串的字符进行就地排序
void sortChars(char* s) {
int len = (int)strlen(s);
// 简单的冒泡排序(长度最多10,足够快)
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - 1 - i; ++j) {
if (s[j] > s[j + 1]) {
char tmp = s[j];
s[j] = s[j + 1];
s[j + 1] = tmp;
}
}
}
}
int main(void) {
int n, k;
char words[MAXN][MAXLEN]; // 输入的 n 个字符串
char x[MAXLEN]; // 目标字符串
char brothers[MAXN][MAXLEN]; // 存放兄弟单词
int brotherCount = 0;
// 按题意:一行输入 n, n个字符串, x, k
if (scanf("%d", &n) != 1) {
return 0;
}
for (int i = 0; i < n; ++i) {
scanf("%s", words[i]);
}
scanf("%s", x);
scanf("%d", &k);
// 预处理目标字符串:记录长度,排序好的版本
int lenx = (int)strlen(x);
char sortedX[MAXLEN];
strcpy(sortedX, x);
sortChars(sortedX);
// 找出所有兄弟单词
for (int i = 0; i < n; ++i) {
// 长度必须相同,且不能与 x 完全一样
if ((int)strlen(words[i]) != lenx) {
continue;
}
if (strcmp(words[i], x) == 0) {
continue;
}
// 排序后比较
char tmp[MAXLEN];
strcpy(tmp, words[i]);
sortChars(tmp);
if (strcmp(tmp, sortedX) == 0) {
// 是兄弟单词
strcpy(brothers[brotherCount], words[i]);
brotherCount++;
}
}
// 输出兄弟单词数量
printf("%d\n", brotherCount);
// 如果存在第 k 小兄弟单词,则输出
if (brotherCount >= k && k >= 1) {
// 按字典序排序兄弟单词数组
qsort(brothers, brotherCount, sizeof(brothers[0]), cmpStr);
printf("%s\n", brothers[k - 1]);
}
return 0;
}
查看8道真题和解析