题目描述
给定用户密码输入流 input,输入流中字符 '<' 表示退格,可以清除前一个输入的字符,请你编写程序,输出最终得到的密码字符,并判断密码是否满足如下的密码安全要求。
密码安全要求如下:
- 密码长度 ≥ 8;
- 密码至少需要包含 1 个大写字母;
- 密码至少需要包含 1 个小写字母;
- 密码至少需要包含 1 个数字;
- 密码至少需要包含 1 个字母和数字以外的非空白特殊字符;
注意空串退格后仍然为空串,且用户输入的字符串不包含 '<' 字符和空白字符。
输入描述
用一行字符串表示输入的用户数据,输入的字符串中 '<' 字符标识退格,用户输入的字符串不包含空白字符,例如:
ABC<c89%000<
输出描述
输出经过程序处理后,输出的实际密码字符串,并输出改密码字符串是否满足密码安全要求。两者间由 ',' 分隔, 例如:
ABc89%00,true
用例
输入 | ABC<c89%000< |
输出 | ABc89%00,true |
说明 | 多余的C和0由于退格被去除,最终用户输入的密码为ABc89%00,且满足密码安全要求,输出true |
题目解析
本题可以利用栈的压栈来模拟用户输入字符,弹栈来模拟'<'退格操作。
最后统计栈中:所有字符数量>=8 && 小写字母数量 >= 1 && 大写字母数量 >= 1 && 数字数量 >= 1 && 非字母数字空白字符数量 >= 1
若满足,则将栈中字符拼接为字符串后再追加“true”,否则追加“false”
JavaScript算法源码
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
const s = await readline();
const stack = [];
for (let c of s) {
if (c == "<") {
if (stack.length == 0) continue;
stack.pop();
} else {
stack.push(c);
}
}
let upper = 0;
let lower = 0;
let number = 0;
let non_letter_number = 0;
const password = [];
for (let c of stack) {
password.push(c);
if (c >= "a" && c <= "z") {
lower++;
} else if (c >= "A" && c <= "Z") {
upper++;
} else if (c >= "0" && c <= "9") {
number++;
} else {
non_letter_number++;
}
}
if (
password.length >= 8 &&
lower >= 1 &&
upper >= 1 &&
non_letter_number >= 1
) {
password.push(",true");
} else {
password.push(",false");
}
console.log(password.join(""));
})();
Java算法源码
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
LinkedList<Character> stack = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '<') {
if (stack.isEmpty()) continue;
stack.removeLast();
} else {
stack.addLast(c);
}
}
int upper = 0;
int lower = 0;
int number = 0;
int non_letter_number = 0;
StringBuilder password = new StringBuilder();
for (Character c : stack) {
password.append(c);
if (c >= 'a' && c <= 'z') {
lower++;
} else if (c >= 'A' && c <= 'Z') {
upper++;
} else if (c >= '0' && c <= '9') {
number++;
} else {
non_letter_number++;
}
}
if (password.length() >= 8
&& lower >= 1
&& upper >= 1
&& number >= 1
&& non_letter_number >= 1) {
password.append(",true");
} else {
password.append(",false");
}
System.out.println(password);
}
}
Python算法源码
# 输入获取
s = input()
# 算法入口
def getResult():
stack = []
for c in s:
if c == '<':
if len(stack) > 0:
stack.pop()
else:
stack.append(c)
upper = 0
lower = 0
number = 0
non_letter_number = 0
password = []
for c in stack:
password.append(c)
if 'z' >= c >= 'a':
lower += 1
elif 'Z' >= c >= 'A':
upper += 1
elif '9' >= c >= '0':
number += 1
else:
non_letter_number += 1
if len(password) >= 8 and lower >= 1 and upper >= 1 and number >= 1 and non_letter_number >= 1:
password.append(",true")
else:
password.append(",false")
return "".join(password)
# 算法调用
print(getResult())
C算法源码
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100000
int main() {
char s[MAX_SIZE];
gets(s);
unsigned long long len = strlen(s);
char stack[MAX_SIZE];
int stack_size = 0;
int upper = 0;
int lower = 0;
int number = 0;
int non_letter_number = 0;
for (int i = 0; i < len; i++) {
char key;
int val;
if (s[i] == '<') {
if (stack_size > 0) {
key = stack[stack_size - 1];
val = -1;
stack_size--;
} else {
continue;
}
} else {
key = s[i];
val = 1;
stack[stack_size++] = s[i];
}
if (key >= 'a' && key <= 'z') {
lower += val;
} else if (key >= 'A' && key <= 'Z') {
upper += val;
} else if (key >= '0' && key <= '9') {
number += val;
} else {
non_letter_number += val;
}
}
stack[stack_size] = '