news 2026/5/17 1:03:57

(新卷、100分)-敏感字段加密(JavaPythonJSC++C)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(新卷、100分)-敏感字段加密(JavaPythonJSC++C)

题目描述

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

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

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

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

输入描述

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

输出描述

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

示例1

输入

输出

说明

示例2

输入

输出

说明

解题思路

题意

就是查找替换的事!

题目要求

示例解释

示例1

输入:

输出:

解释:

示例2

输入:

输出:

解释:

思路

  1. 初始化变量:

  2. 遍历字符数组并处理每个字符:

  3. 检查命令字索引K是否超出范围:

  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()); } } }

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)

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(); }); });

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; }

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; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/14 21:05:47

(新卷,100分)-数大雁(JavaPythonJSC++C)

题目描述 一群大雁往南飞&#xff0c;给定一个字符串记录地面上的游客听到的大雁叫声&#xff0c;请给出叫声最少由几只大雁发出。 具体的: ​ 1.大雁发出的完整叫声为”quack“&#xff0c;因为有多只大雁同一时间嘎嘎作响&#xff0c;所以字符串中可能会混合多个”quack”…

作者头像 李华
网站建设 2026/5/14 1:31:11

飞腾系列——FT-M6678模板匹配算法的实现与优化

全文概述 本文针对国产高性能多核DSP芯片FT-M6678的架构特性,对基于相关系数的模板匹配算法进行移植与优化。研究背景源于传统图像处理算法在M6678平台上的效率低下问题,通过结合算法特性与硬件架构优势,提出并行化与局部性优化方案。核心工作包括:1)基于积分图与FFT的算…

作者头像 李华
网站建设 2026/5/4 12:13:31

[微机原理与系统设计-从入门到入土] 输入输出IO

[微机原理与系统设计-从入门到入土] 输入输出IO 知乎&#xff1a;https://www.zhihu.com/people/byzh_rc CSDN&#xff1a;https://blog.csdn.net/qq_54636039 注&#xff1a;本文仅对所述内容做了框架性引导&#xff0c;具体细节可查询其余相关资料or源码 参考文章&#x…

作者头像 李华
网站建设 2026/5/10 16:12:39

【AI】Cursor 编辑器使用指南

Cursor 编辑器使用指南 &#x1f4d6; 目录 简介核心功能AI 交互模式模型选择快捷工具栏Web 搜索功能引用功能详解快捷键大全Agent 高级功能使用建议总结参考资源 简介 Cursor 是一款由 AI 驱动的代码编辑器&#xff0c;能够理解你的代码库&#xff0c;并通过自然语言助你更…

作者头像 李华
网站建设 2026/5/7 4:27:43

数通设备堆叠技术:iStack与CSS方案对比及应用选型

在数通网络架构中,堆叠技术是提升设备扩展性、可靠性与管理效率的核心方案,其中iStack(华为设备堆叠协议)与CSS(集群交换系统,华为高端设备堆叠方案)是业界主流的两种堆叠实现方式。结合业务口堆叠、堆叠卡堆叠、免配置堆叠等不同部署形态,二者在技术原理、性能特性、适…

作者头像 李华