【华为机考真题】字符串压缩

这里写自定义目录标题

部分通过,请问该咋改

给定一段英文句子和一个英文单词列表。英文句子包含英文单词和标点符号,
其中:
1)英文单词只包含[a-zA-Z]范围内的字符
2)标点符号包括逗号,句号,双引号(双引号两边至少有一个空格)
如果列表中有单词在句子中存在(大小写不敏感)且该单词未被双引号包含,则使用该单词在列表中的索引值(索引值从0开始)代替句子中的该单词
如果英文单词列表存在重复的英文单词,则该单词最后出现英文单词
解答要求:
时间限制:C/C++400ms,其他语言:800ms
内存限制:C/c++200MB,其他语言:400MB

输入:
第一行,一段英文句子
第二行,英文单词列表

提示:每个英文单词长度在[1-50]范围内
输入的英文句子长度在[0,10000]范围内
输入英文单词列表长度在[0,10000]范围内
英文句子不会出现双引号不匹配的情况

输出:
替换后的英文句子

样例1

输入: Hello world.
      Good Hello LOOP
输出: 1 world.
解释:hello在英文句子中存在,则使用hello的索引值进行替换,得到结果1 world.```、
样例2

```bash
输入: 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.
解释:字符串列表中的introduction,IS在句子中存在,first虽然在句子中存在但被双引号包含了,所以使用introduction单词,IS单词(最后一次出现)的索引值进行替换,得到的结果为 An 5 6 "the first paragraph " of your papger

本人写的代码如下,但只是部分用例,还需修改,但不知如何进行修改,请有经验的同学解答一下。
部分通过代码如下所示:

import java.util.*;
import java.io.*;
public class Test {

    private  static  String replaceWithIndex(String sentences,List<String> worldlist){

        String[] words = sentences.split("\\s+");
        StringBuilder replaceSentence = new StringBuilder();
        for (String word: words){
            // word = word.replaceAll("[^a-zA-Z]","");
            int index= worldlist.indexOf(word);
            if(index != -1){
                replaceSentence.append(index).append(" ");
            }else {
                replaceSentence.append(word).append(" ");
            }
        }
        return replaceSentence.toString().trim();
    }




    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);

        String sentences = sc.nextLine();
        String str = sc.nextLine();
        List<String> wordList = new ArrayList<>();
        for(String word : str.split("\\s+") ){

            wordList.add(word);

        }


        String replaceSentences = replaceWithIndex(sentences,wordList);
        System.out.println(replaceSentences);
        sc.close();

    }
}




相关推荐

  1. 华为字符串压缩

    2024-07-18 19:12:04       19 阅读
  2. 华为 -- 游戏分组

    2024-07-18 19:12:04       21 阅读
  3. 华为 -- 篮球游戏

    2024-07-18 19:12:04       21 阅读
  4. 华为 -- 密码解密

    2024-07-18 19:12:04       18 阅读
  5. 华为试题-字符串压缩

    2024-07-18 19:12:04       36 阅读
  6. 华为 -- 攀登者1

    2024-07-18 19:12:04       23 阅读
  7. 华为 -- 螺旋数字矩阵

    2024-07-18 19:12:04       19 阅读
  8. 华为 -- 精准核酸检测

    2024-07-18 19:12:04       20 阅读
  9. 华为 C++ 实现【字符串重新排列】

    2024-07-18 19:12:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 19:12:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 19:12:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 19:12:04       45 阅读
  4. Python语言-面向对象

    2024-07-18 19:12:04       55 阅读

热门阅读

  1. Android init常用调试工具

    2024-07-18 19:12:04       17 阅读
  2. 探索HiFi智能编解码器的奇妙世界

    2024-07-18 19:12:04       15 阅读
  3. 大话设计模式

    2024-07-18 19:12:04       14 阅读