题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <stdio.h> #include <string.h> int comper(char *self, char *str2){ int arr[128] = {0}; int len1 = strlen(self); if(strlen(str2) == len1 && strcmp(self, str2)){ for(int i = 0; i < len1; i++){ arr[self[i]]++; arr[str2[i]]--; } for(int i = 0; i < len1; i++){ if(arr[self[i]] != 0 || arr[str2[i]] != 0){ return 0; } } return 1; } return 0; } int main(){ char str[1004][12]; memset(str, '\0', sizeof(str)); int n = 0; scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%s", str[i]); } char spe[12]; int k; scanf("%s %d", spe, &k); int len = strlen(spe); char *tmp[n]; int count = 0; for(int i = 0; i < n; i++){ if(comper(spe, str[i])){ tmp[count++] = str[i]; } } for(int i = 0; i < count - 1; i++){ for(int j = 0; j < count - 1 - i; j++){ if(strcmp(tmp[j], tmp[j + 1]) > 0){ char *s; s = tmp[j + 1]; tmp[j + 1] = tmp[j]; tmp[j] = s; } } } printf("%d\n", count); if(count > k) printf("%s", tmp[k - 1]); return 0; }