猜字谜 华为OD
源码 Java
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class GuessWord {
public Input input;
@BeforeEach
public void init() {
input= new Input("bdni,wooood\nbind,wrong,wood");
}
@Test
public void test() {
String string = input.nextLine();
String[] words = string.split(",");
String[] answers = input.nextLine().split(",");
List<String> wordList = new ArrayList<>();
List<String> answerList = new ArrayList<>();
for (String word : words) {
wordList.add(dealWord(word));
}
for (String answer : answers) {
answerList.add(dealWord(answer));
}
List<String> result = new ArrayList<>();
for (int i = 0; i < wordList.size(); i++) {
int index = findWord(answerList, wordList.get(i));
if (index != -1) {
result.add(answers[i]);
} else {
result.add("not found");
}
}
System.out.println(String.join(",", result));
}
public int findWord(List<String> lists , String word) {
for (int i = 0; i < lists.size(); i++) {
if (lists.get(i).equals(word)) {
return i;
}
}
return -1;
}
public String dealWord(String word) {
TreeSet<Character> set = new TreeSet<>();
for (int i = 0; i < word.length(); i++) {
set.add(word.charAt(i));
}
Character[] array = set.toArray(new Character[set.size()]);
StringBuilder answer = new StringBuilder();
for (int i = 0; i < array.length; i++) {
answer.append(array[i]);
}
return answer.toString();
}
}
原文地址:https://blog.csdn.net/TangKenny/article/details/143467607
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!