1 条题解
-
0
方法思路
要解决这个问题,我们需要确定小美在尝试所有可能的密码时,最少和最多需要尝试多少次才能成功登录。关键在于理解密码的尝试顺序和如何计算最小和最大尝试次数。
- 密码分组和唯一性处理:首先,将所有密码按长度分组,并确保每个密码只被考虑一次。使用集合来存储每个长度对应的密码,这样可以自动处理重复的密码。
- 检查正确密码是否存在:如果正确的密码不在给定的密码列表中,直接返回
-1 -1
。 - 计算最小尝试次数:最小尝试次数发生在正确密码在相同长度的密码中第一个被尝试的情况下。因此,最小尝试次数等于所有比正确密码短的密码数量加1(即第一次尝试就成功)。
- 计算最大尝试次数:最大尝试次数发生在正确密码在相同长度的密码中最后一个被尝试的情况下。因此,最大尝试次数等于所有比正确密码短的密码数量加上相同长度的密码数量(即所有其他相同长度的密码都被尝试后才尝试正确的密码)。
代码实现
Java
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String correctPassword = br.readLine(); // 使用TreeMap确保按长度排序 TreeMap<Integer, Integer> lengthCount = new TreeMap<>(); boolean containsCorrect = false; int correctLength = correctPassword.length(); int sameLengthCount = 0; Set<String> uniquePasswords = new HashSet<>(); for (int i = 0; i < n; i++) { String password = br.readLine(); if (uniquePasswords.add(password)) { // 只处理唯一的密码 int length = password.length(); lengthCount.put(length, lengthCount.getOrDefault(length, 0) + 1); if (password.equals(correctPassword)) { containsCorrect = true; } if (length == correctLength) { sameLengthCount++; } } } if (!containsCorrect) { System.out.println("-1 -1"); return; } int min = 1, max = 1; // 计算最小尝试次数 for (Map.Entry<Integer, Integer> entry : lengthCount.entrySet()) { if (entry.getKey() < correctLength) { min += entry.getValue(); } else if (entry.getKey() == correctLength) { break; // 在最好情况下,第一个尝试的就是正确密码 } } // 计算最大尝试次数 for (Map.Entry<Integer, Integer> entry : lengthCount.entrySet()) { if (entry.getKey() < correctLength) { max += entry.getValue(); } else if (entry.getKey() == correctLength) { max += sameLengthCount - 1; // 在最坏情况下,最后一个尝试的才是正确密码 break; } } System.out.println(min + " " + max); } }
Python
n = int(input()) correct_password = input() length_to_passwords = {} contains_correct = False for _ in range(n): password = input() length = len(password) if length not in length_to_passwords: length_to_passwords[length] = set() length_to_passwords[length].add(password) if password == correct_password: contains_correct = True if not contains_correct: print("-1 -1") else: min_attempts = 1 max_attempts = 1 correct_length = len(correct_password) # 计算最小尝试次数 for length in length_to_passwords: if length < correct_length: min_attempts += len(length_to_passwords[length]) # 计算最大尝试次数 for length in length_to_passwords: if length < correct_length: max_attempts += len(length_to_passwords[length]) elif length == correct_length: max_attempts += len(length_to_passwords[length]) - 1 print(f"{min_attempts} {max_attempts}")
C++
#include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; int main() { int n; cin >> n; string correctPassword; cin >> correctPassword; unordered_map<int, unordered_set<string>> lengthToPasswords; bool containsCorrect = false; for (int i = 0; i < n; i++) { string password; cin >> password; int length = password.length(); lengthToPasswords[length].insert(password); if (password == correctPassword) { containsCorrect = true; } } if (!containsCorrect) { cout << "-1 -1" << endl; return 0; } int minAttempts = 1; int maxAttempts = 1; int correctLength = correctPassword.length(); // 计算最小尝试次数 for (const auto& pair : lengthToPasswords) { if (pair.first < correctLength) { minAttempts += pair.second.size(); } } // 计算最大尝试次数 for (const auto& pair : lengthToPasswords) { if (pair.first < correctLength) { maxAttempts += pair.second.size(); } else if (pair.first == correctLength) { maxAttempts += pair.second.size() - 1; } } cout << minAttempts << " " << maxAttempts << endl; return 0; }
- 1
信息
- ID
- 36
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 5
- 已通过
- 1
- 上传者