6.10携程笔试[校招]
携程又第N次给我发笔试邀请了,我何德何能来过携程的笔试啊(太菜了),这一次是我最接近AC的一次┮﹏┭
在结束后一分钟内做出来了,结果提交不了了,真可惜。第二题考的是动态规划,A个测试用例也能有20%...
没刷题目的下场就是被笔试虐了,大家记得去刷刷算法,不然笔试都过不了。
分享下第一题的解法,大家可以去跑跑,是通过的:
package 春招笔试题.携程春招4;
import java.util.*;
/**
* @author deepinsea
* @data 2021/6/10 19:11
* 时间限制: 3000MS
* 内存限制: 589824KB
* 题目描述:
* 苏克·冯·望杜是印度著名的人工智能科学家。你正在试着阅读他的文章。但望杜写文章有个习惯,就是喜欢自己造词。他会利用现有的单词拼接上英语的一些前缀和后缀来快速造出一些新词。有些新词的组合十分复杂,读上去让人头大。作为程序员的你,很自然的想到可以写段代码把这些复杂的单词展开成词组以便理解。
* <p>
* 已知的单词前缀和对应的含义有:
* anti<word>: against <word>
* post<word>: after <word>
* pre<word>: before <word>
* re<word>: <word> again
* un<word>: not <word>
* <p>
* 已知的单词后缀和对应的含义有:
* <word>er: one who <word>s
* <word>ing: to actively <word>
* <word>ize: change into <word>
* <word>tion: the process of <word>ing
* <word>ful: full of <word>
* 注意:后缀与原单词的关系更加紧密,所以需要最后展开。如果同时存在多个前缀和后缀,应以原单词为中心,前缀从右到左、后缀从左到右依次展开。
* 举个例子,unmodelize 的展开顺序如下:
* unmodelize
* not modelize
* not change into model
* <p>
* 虽然以上定义不是完全准确,但阅读起来不影响理解。
* <p>
* 输入描述
* 输入信息为一个待展开的单词,所有字母均为小写,不含空格
* <p>
* 输出描述
* 按照题意进行展开后的单词
*/
public class Exam1XieCheng {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String res;
String _word;
try {
_word = in.nextLine();
} catch (Exception e) {
_word = null;
}
res = unwangdulize(_word);
System.out.println(res);
}
/**
* 合成词展开
*
* @param word
* @return
*/
static String unwangdulize(String word) {
// 实现思路:按照前缀与后缀的字符数将word分别切割,然后进行匹配判断,如果是的则将匹配的部分转化为译文
// 截取区间是左闭右开的
// 前缀
String sub1 = word.substring(0, 2);
String sub2 = word.substring(0, 3);
String sub3 = word.substring(0, 4);
// 后缀
String sub4 = word.substring(word.length() - 2, word.length());
String sub5 = word.substring(word.length() - 3, word.length());
String sub6 = word.substring(word.length() - 4, word.length());
List list = new ArrayList();
list.add(sub1);
list.add(sub2);
list.add(sub3);
list.add(sub4);
list.add(sub5);
list.add(sub6);
// System.out.println(list);
// 匹配的库
Map<String, String> map1 = new HashMap<>();
map1.put("anti", "against ");
map1.put("post", "after ");
map1.put("pre", "before ");
map1.put("re", "word ");
map1.put("un", "not ");
// 后缀
Map<String, String> map2 = new HashMap<>();
map2.put("er", "one who ");
map2.put("ing", "to actively ");
map2.put("ize", "change into ");
map2.put("tion", "the process of ");
map2.put("ful", "full of ");
// System.out.println(map1);
// unmodelize
// 前缀匹配
for (String key : map1.keySet()) {
if (list.contains(key)) {
// keySet用for循环遍历key
// System.out.println("word中包含前缀:" + key);
String sub = word.substring(key.length(), word.length());
// 为了保证前缀与后缀都需要在前面,所以记录前缀的长度(长度符号位->关键信息法)
// 这里可以直接根据空格切割(区别标志位法)
// int preLen = key.length();
String replace = map1.get(key) + sub;
word = replace;
// System.out.println(word);
}
}
// 后缀匹配
for (String key : map2.keySet()) {
if (list.contains(key)) {
// keySet用for循环遍历key
// System.out.println("word中包含后缀:" + key);
String[] split = word.split(" ");
String sub = split[1].substring(0, split[1].length() - key.length());
word = split[0] + " " + map2.get(key) + sub;
// System.out.println(word);
}
}
return word;
}
}