题目
给定段英文句子和—个英文单词列表。英文句子包含英文单词和标点符号,其中:
1)英文单词只包含[a-zA-Z]范国内的字符;
2)标点符号包括逗号、句号、双引号(双引号两边至少有一个空格)。
如果列表中有单词在句子中存在(大小写不敏感)且该单词未被双引号包含,则使
用该单词在列表中的索引值(索引值从 O 开始)代替句子中的该单词, 如果英文单词列表中存在重复的英文单词,则以该单词最后一次出现的索引值进
行替换。
解答要求
时间限制:C/IC++400ms,其他语言:800ms
内存限制:C/C++200MB,其他语言:400MB
输入
第一行:一段英文句子第二行:英文单词列表
提示:
每个英文单词长度在[1,50]范围内。
输入的英文句子长度在[0,10000]范围内。
输入的英文单词列表长度在[0,100000]范围内。
英文句子中不会出现双引号不匹配的情况。
输出
替换后的英文句子
样例 1
输入:
Hello world. Good Hello LOOP
输出:1 world. 解释:hello 在英文句子中存在,则使用 hello 的索引值进行替换,得到结果为 1
world. 样例 2
输入:
An introduction is " the first paragraph " of your paper. what say first Second IS introduction IS end
输出: An 5 6 " the first paragraph " of your paper.
参考代码
主要考察的是对字符串的一些操作。
package RealTest;
import java.util.*;
/**
* @ClassName stringCompression
* @Description TODO
* @Author 21916
* @Date 2024/3/13 22:05
*/
public class stringCompression {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String,Integer> map = new HashMap<>();
String str = sc.nextLine();
String words = sc.nextLine();
String[] word = words.split(" ");
for(int i=0;i<word.length;i++){
map.put(word[i].toLowerCase(Locale.ROOT),i);
}
boolean flag= false;
String[] ss =str.split(" ");
for(int i=0;i<ss.length;i++){
if(flag==true) continue;
if(ss[i].equals('"'+"")){
flag=!flag;
continue;
}
if(map.containsKey(ss[i].toLowerCase(Locale.ROOT))){
ss[i] = map.get(ss[i].toLowerCase(Locale.ROOT))+"";
}
}
String res = String.join(" ",ss);
System.out.println(res);
}
}