华为机试题-字符串压缩

题目

给定段英文句子和—个英文单词列表。英文句子包含英文单词和标点符号,其中:
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);
    }
}

相关推荐

  1. 华为试题-字符串压缩

    2024-03-14 00:22:02       21 阅读
  2. 华为OD试题字符串变换最小字符串

    2024-03-14 00:22:02       9 阅读
  3. 华为试题-最小矩阵

    2024-03-14 00:22:02       16 阅读
  4. 华为试题-密码输入检测

    2024-03-14 00:22:02       17 阅读
  5. 华为试题之删除最少字符

    2024-03-14 00:22:02       15 阅读
  6. 牛客网华为试题说明三

    2024-03-14 00:22:02       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 00:22:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 00:22:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 00:22:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 00:22:02       18 阅读

热门阅读

  1. 测开面经学习笔记

    2024-03-14 00:22:02       19 阅读
  2. C++进阶学习

    2024-03-14 00:22:02       21 阅读
  3. 车规芯片为什么需要信息安全(2)

    2024-03-14 00:22:02       21 阅读
  4. 10个必知必会的SQL聚合函数

    2024-03-14 00:22:02       17 阅读
  5. Git命令(持续更新中...)

    2024-03-14 00:22:02       21 阅读
  6. C++:构造函数赋初值的几种形式

    2024-03-14 00:22:02       19 阅读
  7. 蓝桥杯 连号区间数

    2024-03-14 00:22:02       20 阅读