E卷-(100分)敏感字段加密
敏感字段加密
问题描述
给定一个由多个命令字组成的命令字符串 和一个索引 ,要求对指定索引的敏感字段进行加密处理。命令字符串具有以下特征:
- 字符串长度不超过 字节,仅包含大小写字母、数字、下划线和偶数个双引号。
- 命令字之间以一个或多个下划线 "_" 分隔。
- 两个双引号 """" 可用来标识包含下划线的命令字或空命令字(仅包含两个双引号的命令字)。双引号不会在命令字内部出现。
要求对索引 的敏感字段进行加密,将其替换为 "******"(6个星号),并删除命令字前后多余的下划线。如果无法找到指定索引的命令字,则输出字符串 "ERROR"。
输入格式
输入包含两行: 第一行为一个整数 ,表示要加密的命令字索引(从 开始)。 第二行为命令字符串 。
输出格式
输出一行字符串,为处理后的命令字符串。如果无法找到指定索引的命令字,输出 "ERROR"。
样例输入
输入样例1:
1
password__a12345678_timeout_100
输入样例2:
2
aaa_password_"a12_45678"_timeout__100_""_
样例输出
输出样例1:
password_******_timeout_100
输出样例2:
aaa_password_******_timeout_100_""
样例解释
对于样例1,索引 的命令字 "a12345678" 被替换为 "******",并删除了多余的下划线。
对于样例2,索引 的命令字 "a12_45678"(包含在双引号内)被替换为 "******",并删除了多余的下划线。注意最后的空命令字 """" 被保留。
数据范围
- 字符串长度:
- 索引 : 命令字数量
题解
这道题目的核心在于正确解析命令字符串,并对指定索引的命令字进行加密处理。
-
字符串解析:
- 核心思想是使用状态机的概念。我们用一个布尔变量
in_quotes
来跟踪当前是否在引号内。 - 遍历字符串的每个字符,根据当前字符和
in_quotes
的状态来决定如何处理:- 遇到引号时,切换
in_quotes
的状态。 - 在引号外遇到下划线时,表示一个单词的结束。
- 其他情况下,将字符添加到当前单词。
- 遇到引号时,切换
- 核心思想是使用状态机的概念。我们用一个布尔变量
-
数据结构选择:
- 使用数组或列表来存储解析出的单词。这样可以方便地按索引访问和修改单词。
-
索引验证:
- 在替换单词之前,检查给定的索引是否有效(是否在单词列表的范围内)。
- 如果索引无效,直接返回 "ERROR"。
-
单词替换:
- 将指定索引的单词替换为 "******",不考虑原单词是否带引号。
-
结果重构:
- 使用下划线连接处理后的单词列表。
- 去除结果字符串首尾的多余下划线。这可以通过字符串操作或正则表达式实现。
-
边界情况处理:
- 处理空字符串或只有下划线的字符串。
- 正确处理带引号的空单词("")。
参考代码
- Python
import sys
def process_command(index, command):
words = []
current_word = ""
in_quotes = False
# 遍历命令字符串的每个字符
for char in command:
if char == '"':
# 切换引号状态
in_quotes = not in_quotes
current_word += char
elif char == '_' and not in_quotes:
# 如果不在引号内遇到下划线,则结束当前单词
if current_word:
words.append(current_word)
current_word = ""
else:
# 将字符添加到当前单词
current_word += char
# 添加最后一个单词(如果存在)
if current_word:
words.append(current_word)
# 检查索引是否有效
if index < 0 or index >= len(words):
return "ERROR"
# 替换指定索引的单词为加密字符串
words[index] = '******'
# 用下划线连接所有单词,并去除首尾的下划线
result = '_'.join(words)
return result.strip('_')
# 读取输入
index = int(sys.stdin.readline().strip())
command = sys.stdin.readline().strip()
# 处理命令并输出结果
print(process_command(index, command))
- C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_LEN 128
char* process_command(int index, char* command) {
static char result[MAX_LEN];
char words[MAX_LEN][MAX_LEN] = {0};
int word_count = 0;
int len = strlen(command);
bool in_quotes = false;
int start = 0;
// 解析命令字符串
for (int i = 0; i <= len; i++) {
if (command[i] == '"') {
in_quotes = !in_quotes;
} else if ((command[i] == '_' && !in_quotes) || command[i] == '\0') {
if (i > start) {
// 提取单词
strncpy(words[word_count], command + start, i - start);
words[word_count][i - start] = '\0';
word_count++;
}
start = i + 1;
}
}
// 检查索引是否有效
if (index < 0 || index >= word_count) {
return "ERROR";
}
// 替换指定索引的单词为加密字符串
strcpy(words[index], "******");
// 重构结果字符串
result[0] = '\0';
for (int i = 0; i < word_count; i++) {
if (i > 0 && result[strlen(result)-1] != '_') strcat(result, "_");
strcat(result, words[i]);
}
// 删除首尾的下划线
while (result[0] == '_') memmove(result, result + 1, strlen(result));
int result_len = strlen(result);
while (result_len > 0 && result[result_len - 1] == '_') {
result[--result_len] = '\0';
}
return result;
}
int main() {
int index;
char command[MAX_LEN];
// 读取输入
scanf("%d", &index);
scanf(" %[^\n]", command);
// 处理命令并输出结果
printf("%s\n", process_command(index, command));
return 0;
}
- Javascript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function processCommand(index, command) {
const words = [];
let currentWord = '';
let inQuotes = false;
// 遍历命令字符串的每个字符
for (const char of command) {
if (char === '"') {
// 切换引号状态
inQuotes = !inQuotes;
currentWord += char;
} else if (char === '_' && !inQuotes) {
// 如果不在引号内遇到下划线,则结束当前单词
if (currentWord) {
words.push(currentWord);
currentWord = '';
}
} else {
// 将字符添加到当前单词
currentWord += char;
}
}
// 添加最后一个单词(如果存在)
if (currentWord) {
words.push(currentWord);
}
// 检查索引是否有效
if (index < 0 || index >= words.length) {
return "ERROR";
}
// 替换指定索引的单词为加密字符串
words[index] = '******';
// 用下划线连接所有单词,并去除首尾的下划线
return words.join('_').replace(/^_+|_+$/g, '');
}
// 读取输入并处理
let index;
rl.question('', (answer) => {
index = parseInt(answer);
rl.question('', (command) => {
console.log(processCommand(index, command));
rl.close();
});
});
- Java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static String processCommand(int index, String command) {
List<String> words = new ArrayList<>();
StringBuilder currentWord = new StringBuilder();
boolean inQuotes = false;
// 遍历命令字符串的每个字符
for (char c : command.toCharArray()) {
if (c == '"') {
// 切换引号状态
inQuotes = !inQuotes;
currentWord.append(c);
} else if (c == '_' && !inQuotes) {
// 如果不在引号内遇到下划线,则结束当前单词
if (currentWord.length() > 0) {
words.add(currentWord.toString());
currentWord = new StringBuilder();
}
} else {
// 将字符添加到当前单词
currentWord.append(c);
}
}
// 添加最后一个单词(如果存在)
if (currentWord.length() > 0) {
words.add(currentWord.toString());
}
// 检查索引是否有效
if (index < 0 || index >= words.size()) {
return "ERROR";
}
// 替换指定索引的单词为加密字符串
words.set(index, "******");
// 用下划线连接所有单词,并去除首尾的下划线
return String.join("_", words).replaceAll("^_+|_+$", "");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取输入
int index = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
String command = scanner.nextLine();
// 处理命令并输出结果
System.out.println(processCommand(index, command));
scanner.close();
}
}
- Cpp
#include <iostream>
#include <vector>
#include <string>
#include <regex>
using namespace std;
string process_command(int index, const string& command) {
vector<string> words;
string current_word;
bool in_quotes = false;
// 遍历命令字符串的每个字符
for (char c : command) {
if (c == '"') {
// 切换引号状态
in_quotes = !in_quotes;
current_word += c;
} else if (c == '_' && !in_quotes) {
// 如果不在引号内遇到下划线,则结束当前单词
if (!current_word.empty()) {
words.push_back(current_word);
current_word.clear();
}
} else {
// 将字符添加到当前单词
current_word += c;
}
}
// 添加最后一个单词(如果存在)
if (!current_word.empty()) {
words.push_back(current_word);
}
// 检查索引是否有效
if (index < 0 || index >= words.size()) {
return "ERROR";
}
// 替换指定索引的单词为加密字符串
words[index] = "******";
// 重构结果字符串
string result;
for (const auto& word : words) {
if (!result.empty()) result += "_";
result += word;
}
// 去除首尾的下划线
return regex_replace(result, regex("^_+|_+$"), "");
}
int main() {
int index;
string command;
// 读取输入
cin >> index;
cin.ignore(); // 忽略换行符
getline(cin, command);
// 处理命令并输出结果
cout << process_command(index, command) << endl;
return 0;
}
#OD#OD刷题笔记 文章被收录于专栏
本专栏收集并整理了一些刷题笔记