news 2026/3/10 15:55:43

(新卷、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/3/7 2:55:52

【完全免费】小丸工具箱超详细的视频体积压缩教程,从1G视频压缩到100M,几乎不损画质,完全免费,而且可以批量处理视频文件

——软件使用教程—— 小丸工具箱超详细的视频体积压缩教程&#xff0c;从1G视频压缩到100M——下载地址&#xff08;防止被拦截&#xff0c;请用浏览器打开&#xff09;—— 夸克地址&#xff1a; https://pan.dxlszyk.com/s/1jeou2e1k 多盘地址&#xff1a; https://www.…

作者头像 李华
网站建设 2026/3/10 3:47:24

【完全免费】黑白照片变彩色照片,一键处理百张黑白老照片,AI上色效果比手工精细10倍,效果太惊艳了,老照片AI修复上色全流程演示,支持离线使用!

——软件使用教程—— 黑白照片变彩色照片&#xff0c;一键处理百张黑白老照片&#xff0c;支持离线使用&#xff01;——下载地址&#xff08;防止被拦截&#xff0c;请用浏览器打开&#xff09;—— 夸克地址&#xff1a; https://pan.dxlszyk.com/s/1jemfj9sk 多盘地址&a…

作者头像 李华
网站建设 2026/3/5 23:29:42

程序员必备的语义检索工具:基于GTE模型的高效相似度计算实践

程序员必备的语义检索工具&#xff1a;基于GTE模型的高效相似度计算实践 在现代信息处理系统中&#xff0c;语义理解能力已成为提升搜索、推荐和问答系统智能化水平的核心要素。传统的关键词匹配方式已难以满足复杂场景下的精准需求&#xff0c;而语义检索技术正逐步成为构建智…

作者头像 李华
网站建设 2026/3/9 21:32:29

一键智能抠图实践|基于CV-UNet大模型镜像快速部署批量处理方案

一键智能抠图实践&#xff5c;基于CV-UNet大模型镜像快速部署批量处理方案 在电商产品图处理、AI图像生成、数字内容创作等场景中&#xff0c;高质量的图像抠图能力已成为基础刚需。传统手动抠图效率低&#xff0c;而市面上多数在线工具存在隐私泄露、成本高、无法批量处理等问…

作者头像 李华
网站建设 2026/3/10 5:25:20

一键批量抠图实践|基于CV-UNet大模型镜像高效实现

一键批量抠图实践&#xff5c;基于CV-UNet大模型镜像高效实现 1. 引言&#xff1a;智能抠图的工程化落地需求 在电商、广告设计、影视后期等场景中&#xff0c;图像背景移除&#xff08;即“抠图”&#xff09;是一项高频且关键的任务。传统手动抠图效率低、成本高&#xff0…

作者头像 李华