机考E卷100分题 - 敏感字段加密

题目描述

给定一个由多个命令字组成的命令字符串:

1、字符串长度小于等于127字节,只包含大小写字母,数字,下划线和偶数个双引号;
2、命令字之间以一个或多个下划线_进行分割;
3、可以通过两个双引号””来标识包含下划线_的命令字或空命令字(仅包含两个双引号的命令字),双引号不会在命令字内部出现;

请对指定索引的敏感字段进行加密,替换为******(6个*),并删除命令字前后多余的下划线_

如果无法找到指定索引的命令字,输出字符串ERROR。

输入描述

输入为两行,第一行为命令字索引K(从0开始),第二行为命令字符串S。

输出描述

输出处理后的命令字符串,如果无法找到指定索引的命令字,输出字符串ERROR

示例1

输入

1
password__a12345678_timeout_100
12

输出

password_******_timeout_100
1

说明

示例2

输入

2
aaa_password_"a12_45678"_timeout__100_""_
12

输出

aaa_password_******_timeout_100_""
1

说明

解题思路

题意

就是查找替换的事!

题目要求

  • 读取输入中的命令字索引 K(从0开始计数)和命令字符串 S
  • 查找命令字符串 S 中第 K 个命令字,将其替换为 ******(6个 *)。
  • 去除处理后字符串中每个命令字前后的多余下划线 _
  • 如果找不到指定索引的命令字,则输出字符串 "ERROR"

示例解释

示例1

输入:

1
password__a12345678_timeout_100
12

输出:

password_******_timeout_100
1

解释:

  • 1 个命令字是 a12345678,用 ****** 替换。
  • 处理后字符串为 password_******_timeout_100
示例2

输入:

2
aaa_password_"a12_45678"_timeout__100_""_
12

输出:

aaa_password_******_timeout_100_""
1

解释:

  • 2 个命令字是 "a12_45678",用 ****** 替换。
  • 删除处理后的字符串中每个命令字前后的多余下划线 _
  • 结果为 aaa_password_******_timeout_100_""

思路

  1. 初始化变量:

    • 定义一个空字符串 command 来存储当前正在解析的命令字。
    • 定义一个列表 commandList 来存储解析后的命令字列表。
  2. 遍历字符数组并处理每个字符:

    • a. 如果当前字符是双引号且 command 中已经包含一个双引号:
      • 将双引号添加到 command 中。
      • 将解析完毕的命令字添加到 commandList 中,然后重置 command
    • b. 如果 command 不包含双引号且当前字符是下划线:
      • 检查 command 是否为空,如果不为空:
        • 将解析完毕的命令字添加到 commandList 中,然后重置 command
    • c. 如果已经到达字符串末尾:
      • 将当前字符添加到 command 中。
      • 将解析完毕的命令字添加到 commandList 中,然后重置 command
    • d. 否则,将当前字符添加到 command 中。
  3. 检查命令字索引 K 是否超出范围:

    • 如果超出范围,输出 "ERROR"
  4. 否则:

    • 将指定索引的命令字替换为 "******",并构建结果字符串。

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int index = Integer.parseInt(scanner.nextLine()); // 输入命令字索引K
        String input = scanner.nextLine(); // 输入命令字符串S
        char[] charArray = input.toCharArray(); // 将命令字符串转换为字符数组
        String command = ""; // 当前正在解析的命令字
        List<String> commandList = new ArrayList<>(); // 存储解析后的命令字列表

        for (int i = 0; i < charArray.length; i++) {
            char ch = charArray[i];

            if (ch == '"' && command.contains(ch + "")) { // 如果当前字符为双引号且命令字中已经包含了一个双引号
                command += '"'; // 将双引号添加到命令字中
                commandList.add(command); // 将解析完毕的命令字添加到命令字列表中
                command = ""; // 重置命令字
            } else if (!command.contains("\"") && ch == '_') { // 如果命令字不包含双引号且当前字符为下划线
                if (!command.isEmpty()) { // 如果命令字不为空
                    commandList.add(command); // 将解析完毕的命令字添加到命令字列表中
                    command = ""; // 重置命令字
                }
            } else if (i == charArray.length - 1) { // 如果已经到达字符串末尾
                command += ch; // 将当前字符添加到命令字中
                commandList.add(command); // 将解析完毕的命令字添加到命令字列表中
                command = ""; // 重置命令字
            } else {
                command += ch; // 将当前字符添加到命令字中
            }
        }

        if (index < 0 || index > commandList.size() - 1) { // 如果命令字索引超出范围
            System.out.println("ERROR");
        } else {
            commandList.set(index, "******"); // 将指定索引的命令字替换为******
            StringBuilder result = new StringBuilder();

            for (String item : commandList) {
                result.append("_").append(item); // 在命令字之前添加下划线
            }

            result.deleteCharAt(0); // 删除第一个下划线
            System.out.println(result.toString());
        }
    }
}

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748

Python

import sys

index = int(input()) # 输入命令字索引K
input = input() # 输入命令字符串S
charArray = list(input) # 将命令字符串转换为字符数组
command = "" # 当前正在解析的命令字
commandList = [] # 存储解析后的命令字列表

for i in range(len(charArray)):
    ch = charArray[i]

    if ch == '"' and ch in command: # 如果当前字符为双引号且命令字中已经包含了一个双引号
        command += '"' # 将双引号添加到命令字中
        commandList.append(command) # 将解析完毕的命令字添加到命令字列表中
        command = "" # 重置命令字
    elif '"' not in command and ch == '_': # 如果命令字不包含双引号且当前字符为下划线
        if command: # 如果命令字不为空
            commandList.append(command) # 将解析完毕的命令字添加到命令字列表中
            command = "" # 重置命令字
    elif i == len(charArray) - 1: # 如果已经到达字符串末尾
        command += ch # 将当前字符添加到命令字中
        commandList.append(command) # 将解析完毕的命令字添加到命令字列表中
        command = "" # 重置命令字
    else:
        command += ch # 将当前字符添加到命令字中

if index < 0 or index > len(commandList) - 1: # 如果命令字索引超出范围
    print("ERROR")
else:
    commandList[index] = "******" # 将指定索引的命令字替换为******
    result = []

    for item in commandList:
        result.append("_" + item) # 在命令字之前添加下划线

    result = "".join(result)
    result = result[1:] # 删除第一个下划线
    print(result)

123456789101112131415161718192021222324252627282930313233343536373839

JavaScript

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.on('line', (index) => {
    rl.on('line', (input) => {
        const charArray = input.split(''); // 将命令字符串转换为字符数组
        let command = ""; // 当前正在解析的命令字
        let commandList = []; // 存储解析后的命令字列表

        for (let i = 0; i < charArray.length; i++) {
            const ch = charArray[i];

            if (ch === '"' && command.includes(ch)) { // 如果当前字符为双引号且命令字中已经包含了一个双引号
                command += '"'; // 将双引号添加到命令字中
                commandList.push(command); // 将解析完毕的命令字添加到命令字列表中
                command = ""; // 重置命令字
            } else if (!command.includes('"') && ch === '_') { // 如果命令字不包含双引号且当前字符为下划线
                if (command !== "") { // 如果命令字不为空
                    commandList.push(command); // 将解析完毕的命令字添加到命令字列表中
                    command = ""; // 重置命令字
                }
            } else if (i === charArray.length - 1) { // 如果已经到达字符串末尾
                command += ch; // 将当前字符添加到命令字中
                commandList.push(command); // 将解析完毕的命令字添加到命令字列表中
                command = ""; // 重置命令字
            } else {
                command += ch; // 将当前字符添加到命令字中
            }
        }

        if (index < 0 || index > commandList.length - 1) { // 如果命令字索引超出范围
            console.log("ERROR");
        } else {
            commandList[index] = "******"; // 将指定索引的命令字替换为******
            let result = "";

            for (let i = 0; i < commandList.length; i++) {
                result += "_" + commandList[i]; // 在命令字之前添加下划线
            }

            result = result.substring(1); // 删除第一个下划线
            console.log(result);
        }

        rl.close();
    });
});

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

C++

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int index;
    cin >> index; // 输入命令字索引K
    string input;
    cin >> input; // 输入命令字符串S
    vector<char> charArray(input.begin(), input.end()); // 将命令字符串转换为字符数组
    string command = ""; // 当前正在解析的命令字
    vector<string> commandList; // 存储解析后的命令字列表

    for (int i = 0; i < charArray.size(); i++) {
        char ch = charArray[i];

        if (ch == '"' && command.find(ch) != string::npos) { // 如果当前字符为双引号且命令字中已经包含了一个双引号
            command += '"'; // 将双引号添加到命令字中
            commandList.push_back(command); // 将解析完毕的命令字添加到命令字列表中
            command = ""; // 重置命令字
        } else if (command.find('"') == string::npos && ch == '_') { // 如果命令字不包含双引号且当前字符为下划线
            if (!command.empty()) { // 如果命令字不为空
                commandList.push_back(command); // 将解析完毕的命令字添加到命令字列表中
                command = ""; // 重置命令字
            }
        } else if (i == charArray.size() - 1) { // 如果已经到达字符串末尾
            command += ch; // 将当前字符添加到命令字中
            commandList.push_back(command); // 将解析完毕的命令字添加到命令字列表中
            command = ""; // 重置命令字
        } else {
            command += ch; // 将当前字符添加到命令字中
        }
    }

    if (index < 0 || index > commandList.size() - 1) { // 如果命令字索引超出范围
        cout << "ERROR" << endl;
    } else {
        commandList[index] = "******"; // 将指定索引的命令字替换为******
        string result = "";

        for (string item : commandList) {
            result += "_" + item; // 在命令字之前添加下划线
        }

        result = result.substr(1); // 删除第一个下划线
        cout << result << endl;
    }

    return 0;
}

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

C语言

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义一个宏,用于最大字符串长度
#define MAX_LEN 128

// 定义一个函数来分割命令字符串
void split_command(char *input, char commandList[][MAX_LEN], int *commandCount) {
    char command[MAX_LEN] = ""; // 当前正在解析的命令字
    int j = 0; // 用于记录命令字的字符位置

    for (int i = 0; i < strlen(input); i++) {
        char ch = input[i];

        // 如果当前字符为双引号且命令字中已经包含了一个双引号
        if (ch == '"' && strchr(command, '"') != NULL) {
            command[j++] = '"'; // 将双引号添加到命令字中
            command[j] = '\0'; // 结束当前命令字字符串
            strcpy(commandList[*commandCount], command); // 将命令字存储到命令列表中
            (*commandCount)++; // 增加命令字计数
            j = 0; // 重置命令字的字符位置
            command[0] = '\0'; // 重置命令字
        }
        // 如果命令字不包含双引号且当前字符为下划线
        else if (strchr(command, '"') == NULL && ch == '_') {
            if (j > 0) { // 如果命令字不为空
                command[j] = '\0'; // 结束当前命令字字符串
                strcpy(commandList[*commandCount], command); // 将命令字存储到命令列表中
                (*commandCount)++; // 增加命令字计数
                j = 0; // 重置命令字的字符位置
                command[0] = '\0'; // 重置命令字
            }
        }
        // 如果已经到达字符串末尾
        else if (i == strlen(input) - 1) {
            command[j++] = ch; // 将当前字符添加到命令字中
            command[j] = '\0'; // 结束当前命令字字符串
            strcpy(commandList[*commandCount], command); // 将命令字存储到命令列表中
            (*commandCount)++; // 增加命令字计数
        }
        // 其他情况下,将当前字符添加到命令字中
        else {
            command[j++] = ch;
        }
    }
}

int main() {
    int index;
    char input[MAX_LEN];
    char commandList[MAX_LEN][MAX_LEN]; // 存储解析后的命令字列表
    int commandCount = 0; // 记录解析到的命令字数

    // 输入命令字索引K
    scanf("%d", &index);
    // 输入命令字符串S
    scanf("%s", input);

    // 将命令字符串转换为命令列表
    split_command(input, commandList, &commandCount);

    // 如果命令字索引超出范围
    if (index < 0 || index >= commandCount) {
        printf("ERROR\n");
    } else {
        // 将指定索引的命令字替换为******
        strcpy(commandList[index], "******");
        
        // 构建结果字符串
        for (int i = 0; i < commandCount; i++) {
            if (i > 0) {
                printf("_"); // 在命令字之间添加下划线
            }
            printf("%s", commandList[i]); // 输出命令字
        }
        printf("\n");
    }

    return 0;
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
#牛客创作赏金赛#

主要记录自己的刷题日常,学而时习之。

全部评论

相关推荐

美团开奖了,谁说测开比后端薪资低?谁说前端比后端薪资低?好了你又要说后端可以争取sp、ssp,但是能拿到美团白菜offer的就已经算是人中龙凤了,拿到sp、ssp更是凤毛麟角!依旧劝退后端!你后端学历内卷炼狱!实习经历卷的爆!甚至无法入行!入行了也只是和测开、前端的一般!1.学历,最痛的一击!后端工程师的第一步,走得不是技术,而是学历!想要进入大厂?好好看清楚自己的身份证:没有名校背景,别想着进美团、字节、腾讯!&nbsp;面试官看你的第一眼就会想:“呵,去,给你点面试机会,看看你的技术!”什么?你说自己有技术?不好意思,来点GitHub链接,Project经历,能让面试官笑着赶你走。你没个985、211,双一流,根本就无法站稳在这场技术竞赛的起点。你想进大厂,没学历,没技术!永远只有一个词——&nbsp;“被无情拒绝”。2.&nbsp;薪资:你不过是和前端、测开的一匹马“后端工程师薪资高?能进SSP就是牛逼!”SSP?&nbsp;听起来像是你梦想的银河,但实际上能拿到这个级别的人&nbsp;凤毛麟角,除非你在面试官面前像神话人物一样打了个响指,否则你连SSP的尾巴都摸不着。至于你说的“前端薪资不高”?别逗了,前端都在笑你呢,&nbsp;他们搞个页面,工资比你写个亿级请求接口还多。你说你辛辛苦苦优化API、调度缓存,别人搞个UI设计就能多拿几千块。前端已经不止是个展示层了,他们赚得比你还轻松,而你不过是服务器上疯狂跑“CRUD操作”的那只笨重的工蚁。3.&nbsp;后端的真正意义:修&nbsp;Bug,解决问题,下一份工作还是修&nbsp;Bug有多少人觉得后端是系统架构、数据库优化的高端战场?醒醒吧!&nbsp;后端的真正使命:维护旧项目,修复别人留下的烂摊子。你觉得自己能构建一个完美的系统?不!你只会一边修复技术债务,一边打着&nbsp;“重构”&nbsp;的旗号,换来的是&nbsp;“重构再重构”&nbsp;的无尽循环。而且,别告诉我你能专心写代码。你又要写代码,又要看服务器日志,没事还得帮别人&nbsp;修崩的数据库,给前端数据源做“格式化”。你就是那块永远处于消耗型工作的&nbsp;“万金油”。4.&nbsp;晋升?哈哈哈,你是在做梦!你以为后端开发是一条顺风顺水的快速晋升路线?错!&nbsp;你永远只能在一个“程序员”的岗位上打转,或者你为自己设立目标:“我要成为架构师”,那真的是在妄想。架构师?高级开发?靠近那条道路,你的心脏会先被晋升难度给捏住,你前方只有一座座高不可攀的技术山。别看那些SSP,架构师,架构啥呀?公司里的架构都是前端架构师,你就坐在后端的角落里,照顾着你那些满是错误的API和服务器。5.&nbsp;加班?还是加班!你以为后端开发能像文艺片那样“偶尔加个班”?哈哈,傻了吧!&nbsp;后端开发的生活是无休止的加班和修bug,你不仅要写接口,还得守夜调度、监控系统性能。就连你写的那个“完美的数据库查询”,也可能在&nbsp;第二天&nbsp;被前端因为“页面卡顿”给打回原形。“没有加班,你还能吃什么饭?”你说你是程序员,结果你的生活全是&nbsp;熬夜加班、调试、重启。前端跑个页面,喝个咖啡就能过关,而你呢,熬夜跟数据库调试,最后还是那个穷忙的死循环。6.&nbsp;技术天花板:架构?技术深度?笑死了!后端开发的天花板?那不过是个永远也摸不着的架构师“梦想”,你能掌握几款框架、几种数据库、两三套微服务架构,最后也不过是个&nbsp;管理端的“搬运工”。你没办法“打破天花板”,更没有机会跳出“自己写个爬虫”或者“API接口”的死循环。技术深度?你也不过是&nbsp;“技术债务”的修复者,一天到晚都在修补“老旧系统”的缺陷,偶尔听前端同学聊聊他们React、Vue的最新版本,你根本无法理解他们说的是什么。
开心小狗🐶:感觉后端有点像考研的0812,报名的时候都想冲0812,看不上0854。但是真入学了,不都是众生平等
点赞 评论 收藏
分享
09-23 14:45
贵州大学 财务
勇敢求职牛牛:怎么9.2佬人手一个中信证券实习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务