华为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 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
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;
}
全部评论
相关推荐
11-04 16:01
山东大学 嵌入式工程师 点赞 评论 收藏
分享
美团 Java后端开发 10w刀 美硕
YamadaAnna:包留美的,你拿的美团 招银,没一个不加班的。考虑一下未来吧,应届生的工资真不重要,10w刀税后6w,省省还是能活下去的。回国了35岁怎么办,难道35岁还能返美么,就算35岁还能在国内找到工作,难道打算一辈子9点10点下班么。你有能力在美利坚找到工作,回国如果不是哪个965大厂给你发个ssp,真不值得。
等抽不中h1b,没办法了再回国吧。
点赞 评论 收藏
分享