给定一个可存储若干单词的字典,找出指定单词的所有相似单词,并且按照单词名称从小到大排序输出。单词仅包括字母,但可能大小写并存(大写不一定只出现在首字母)。
相似单词说明:给定一个单词X
,如果通过任意交换单词中字母的位置得到不同的单词Y
,那么定义Y是X的相似单词,如abc
、bca
即为相似单词(大小写是不同的字母,如a
和A
算两个不同字母)。
字典序排序: 大写字母 < 小写字母
。同样大小写的字母,遵循26字母顺序大小关系。即A < B < C < ... < X < Y < Z < a < b < c < ... < x < y < z
. 如 Bac < aBc < acB < cBa
.
第一行为给定的单词个数 ( 为非负整数)
从第二行到地 行是具体的单词(每行一个单词)
最后一行是指定的待检测单词(用于检测上面给定的单词中哪些是与该指定单词是相似单词,该单词可以不是上面给定的单词)
从给定的单词组中,找出指定单词的相似单词,并且按照从小到大字典序排列输出,中间以空格隔开
如果不存在,则输出null
(字符串null
)
4
abc
dasd
tad
bca
abc
abc bca
在给定的输入中,与abc
是兄弟单词的是abc
bca
,且输出按照字典序大小排序,输出的所有单词以空格隔开
4
abc
dasd
tad
bca
abd
null
给定单词组中,没有与给定单词abd
是兄弟单词,输出为null
(字符串null
)
在这道题目中,我们需要找出给定字典中与指定单词相似的单词。相似的定义是,可以通过交换两个单词中的字母位置来得到。题目要求输出的相似单词按照字典序从小到大排序,如果没有找到任何相似单词,则输出"null"。
为了检查两个单词是否相似,我们可以使用一个计数数组来计算每个字符在两个单词中出现的次数。如果所有字符的计数在两个单词中相等,则这两个单词是相似的。
总结,这道题目主要考察了字符串操作、排序以及遍历的基本技巧。我们需要设计一个高效的算法来检查两个单词是否相似,并利用排序和遍历来找出给定字典中的相似单词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_similar(const char *word1, const char *word2) {
if (strlen(word1) != strlen(word2)) {
return 0;
}
int count[52] = {0};
for (int i = 0; word1[i] != '\0'; i++) {
if ('A' <= word1[i] && word1[i] <= 'Z') {
count[word1[i] - 'A']++;
} else {
count[word1[i] - 'a' + 26]++;
}
if ('A' <= word2[i] && word2[i] <= 'Z') {
count[word2[i] - 'A']--;
} else {
count[word2[i] - 'a' + 26]--;
}
}
for (int i = 0; i < 52; i++) {
if (count[i] != 0) {
return 0;
}
}
return 1;
}
int compare_strings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main() {
int n;
scanf("%d", &n);
char **words = (char **)malloc(n * sizeof(char *));
for (int i = 0; i < n; i++) {
words[i] = (char *)malloc(101 * sizeof(char));
scanf("%s", words[i]);
}
char target_word[101];
scanf("%s", target_word);
qsort(words, n, sizeof(char *), compare_strings);
int found_similar = 0;
for (int i = 0; i < n; i++) {
if (is_similar(words[i], target_word)) {
if (found_similar) {
printf(" ");
}
found_similar = 1;
printf("%s", words[i]);
}
}
if (!found_similar) {
printf("null");
}
printf("\n");
for (int i = 0; i < n; i++) {
free(words[i]);
}
free(words);
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
bool is_similar(const std::string& word1, const std::string& word2) {
if (word1.length() != word2.length()) {
return false;
}
int count[52] = {0};
for (size_t i = 0; i < word1.length(); i++) {
if ('A' <= word1[i] && word1[i] <= 'Z') {
count[word1[i] - 'A']++;
} else {
count[word1[i] - 'a' + 26]++;
}
if ('A' <= word2[i] && word2[i] <= 'Z') {
count[word2[i] - 'A']--;
} else {
count[word2[i] - 'a' + 26]--;
}
}
for (int i = 0; i < 52; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}
int main() {
int n;
std::cin >> n;
std::vector<std::string> words(n);
for (int i = 0; i < n; i++) {
std::cin >> words[i];
}
std::string target_word;
std::cin >> target_word;
std::sort(words.begin(), words.end());
bool found_similar = false;
for (const auto& word : words) {
if (is_similar(word, target_word)) {
if (found_similar) {
std::cout << " ";
}
found_similar = true;
std::cout << word;
}
}
if (!found_similar) {
std::cout << "null";
}
std::cout << std::endl;
return 0;
}
/**
* Created with IntelliJ IDEA.
*
* @Author: Amos
* @E-mail: amos@amoscloud.com
* @Date: 2023/3/5
* @Time: 19:45
* @Description:
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;
public class Main0226 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int n = scanner.nextInt();
String[] words = new String[n];
for (int i = 0; i < n; i++) {
words[i] = scanner.next();
}
String target = scanner.next();
System.out.println(solution(words, target));
}
}
public static String solution(String[] words, String target) {
String sorted_target = sort(target);
ArrayList<String> ans = new ArrayList<>();
for (String word : words) {
String sorted_word = sort(word);
if (sorted_target.equals(sorted_word)) {
ans.add(word);
}
}
if (ans.size() > 0) {
ans.sort(String::compareTo);
StringJoiner sj = new StringJoiner(" ");
for (String an : ans) sj.add(an);
return sj.toString();
} else {
return "null";
}
}
public static String sort(String word) {
char[] chars = word.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
}
def is_similar(word1, word2):
if len(word1) != len(word2):
return False
count = [0] * 52
for c1, c2 in zip(word1, word2):
if 'A' <= c1 <= 'Z':
count[ord(c1) - ord('A')] += 1
else:
count[ord(c1) - ord('a') + 26] += 1
if 'A' <= c2 <= 'Z':
count[ord(c2) - ord('A')] -= 1
else:
count[ord(c2) - ord('a') + 26] -= 1
for c in count:
if c != 0:
return False
return True
n = int(input())
words = [input() for _ in range(n)]
target_word = input()
words.sort()
found_similar = False
for word in words:
if is_similar(word, target_word):
if found_similar:
print(" ", end="")
found_similar = True
print(word, end="")
if not found_similar:
print("null", end="")
print()
function isSimilar(word1, word2) {
if (word1.length !== word2.length) {
return false;
}
let count = Array(52).fill(0);
for (let i = 0; i < word1.length; i++) {
let c1 = word1[i];
let c2 = word2[i];
if ('A' <= c1 && c1 <= 'Z') {
count[c1.charCodeAt() - 'A'.charCodeAt()] += 1;
} else {
count[c1.charCodeAt() - 'a'.charCodeAt() + 26] += 1;
}
if ('A' <= c2 && c2 <= 'Z') {
count[c2.charCodeAt() - 'A'.charCodeAt()] -= 1;
} else {
count[c2.charCodeAt() - 'a'.charCodeAt() + 26] -= 1;
}
}
for (let c of count) {
if (c !== 0) {
return false;
}
}
return true;
}
const input = require('fs').readFileSync('/dev/stdin', 'utf-8').split('\n');
const n = parseInt(input[0]);
const words = input.slice(1, n + 1);
const targetWord = input[n + 1];
words.sort();
let foundSimilar = false;
for (let word of words) {
if (isSimilar(word, targetWord)) {
if (foundSimilar) {
process.stdout.write(" ");
}
foundSimilar = true;
process.stdout.write(word);
}
}
if (!foundSimilar) {
process.stdout.write("null");
}
process.stdout.write("\n");
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
func isSimilar(word1, word2 string) bool {
if len(word1) != len(word2) {
return false
}
count := make([]int, 52)
for i := 0; i < len(word1); i++ {
c1 := word1[i]
c2 := word2[i]
if 'A' <= c1 && c1 <= 'Z' {
count[c1-'A'] += 1
} else {
count[c1-'a'+26] += 1
}
if 'A' <= c2 && c2 <= 'Z' {
count[c2-'A'] -= 1
} else {
count[c2-'a'+26] -= 1
}
}
for _, c := range count {
if c != 0 {
return false
}
}
return true
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
var n int
fmt.Sscan(scanner.Text(), &n)
words := make([]string, n)
for i := 0; i < n; i++ {
scanner.Scan()
words[i] = scanner.Text()
}
sort.Strings(words)
scanner.Scan()
targetWord := scanner.Text()
foundSimilar := false
for _, word := range words {
if isSimilar(word, targetWord) {
if foundSimilar {
fmt.Print(" ")
}
foundSimilar = true
fmt.Print(word)
}
}
if !foundSimilar {
fmt.Print("null")
}
fmt.Println()
}