华为OD机试真题 - 快速人名查找
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] namea = sc.nextLine().split(",");
String abbr = sc.nextLine();
System.out.println(getNames(namea, abbr));
}
public static List getNames(String[] nameall, String word) {
List<String> ans = new ArrayList<>();
for (int i = 0; i < nameall.length; i++) {
if (isSaftty(nameall[i], word))
ans.add(nameall[i]);
}
return ans;
}
public static boolean isSaftty(String name, String word) {
String[] names = name.split( " ");
int index = 0, right = 0, i = 1;
while (right + i < word.length()) {
if (dfs(names, word, index, right, right + i))
return true;
i++;
}
return false;
}
public static boolean dfs(String[] names, String word, int index,int left,int right) {
if (index > names.length - 1 && left > word.length())
return true;
if (index > names.length - 1 || right > word.length())
return false;
String s = word.substring(left, right);
if (names[index].startsWith(s)) {
if (index == names.length - 1 && right == word.length())
return true;
int i = 1;
while (right + i <= word.length()) {
if (dfs(names, word, index + 1, right, right + i))
return true;
i++;
}
return false;
} else
return false;
}
Scanner sc = new Scanner(System.in);
String[] namea = sc.nextLine().split(",");
String abbr = sc.nextLine();
System.out.println(getNames(namea, abbr));
}
public static List getNames(String[] nameall, String word) {
List<String> ans = new ArrayList<>();
for (int i = 0; i < nameall.length; i++) {
if (isSaftty(nameall[i], word))
ans.add(nameall[i]);
}
return ans;
}
public static boolean isSaftty(String name, String word) {
String[] names = name.split( " ");
int index = 0, right = 0, i = 1;
while (right + i < word.length()) {
if (dfs(names, word, index, right, right + i))
return true;
i++;
}
return false;
}
public static boolean dfs(String[] names, String word, int index,int left,int right) {
if (index > names.length - 1 && left > word.length())
return true;
if (index > names.length - 1 || right > word.length())
return false;
String s = word.substring(left, right);
if (names[index].startsWith(s)) {
if (index == names.length - 1 && right == word.length())
return true;
int i = 1;
while (right + i <= word.length()) {
if (dfs(names, word, index + 1, right, right + i))
return true;
i++;
}
return false;
} else
return false;
}
全部评论
相关推荐

点赞 评论 收藏
分享
06-23 12:08
广州大学 硬件测试 小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
点赞 评论 收藏
分享