// 字符串解密
func HW2023021() {
// 测试用例
// let inputStr1 = "123admyffc79pt", inputStr2 = "ssyy"
// let inputStr1 = "123admyffc79ptaagghi2222smeersst88mnrt", inputStr2 = "ssyyfgh"
// let inputStr1 = "abcmnq", inputStr2 = "rt"
// 开始代码
let inputStr1 = String(readLine()!)
let inputStr2 = String(readLine()!)
let diffLen = Set(Array(inputStr2)).count // 参考字符不同字母数量
var resDic: [String: Int] = [:] // 有效字符串保存字典
var temp: String = "" // 临时字符串记录有效字符串
var maxLen = 0 // 有效字符串最大长度
for (i,c) in inputStr1.enumerated() {
if c >= "g" && c <= "z" {
temp.append(String(c))
if i != inputStr1.count - 1 { continue }
}
if !temp.isEmpty {
let len = temp.count
if len <= diffLen && len >= maxLen {
if len > maxLen { // 只保留最大长度的有效字符串
resDic.removeAll()
}
maxLen = max(maxLen, len)
resDic.updateValue(temp.count, forKey: temp) // 保存有效字符串
}
temp = "" // 清空临时字符串,准备拼接下一个有效字符串
}
}
if resDic.count == 0 { // 没有有效字符串就打印Not Found
print("Not Found")
return
}
for (i,dic) in resDic.enumerated() {
if i == resDic.count - 1 {
print(dic.key) // 打印字典序里最后一个有效字符串
}
}
}
网友评论