华为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-04 10:32
安徽大学 单片机 实习僧和BOSS直聘都投了几十家,硬件开发,硬件测试,嵌入式都投了,全是已读不回……我现在考虑想在秋招前速成一个Linux项目,其实现在完全不知道自己要找什么方向的,只能海投了,求大佬们给点意见😭😭😭
西工程小巴:数电课设+数字信号处理课设+微机原理课设?

点赞 评论 收藏
分享