题解

本题要求从多个箱子中找出与给定密码相匹配的箱子编号。每个箱子内包含一个字符串,我们需要提取出所有字母字符,忽略大小写后按升序排列,若与给定密码完全一致,则返回该箱子的编号。如果没有匹配的箱子,则返回 -1。

思路分析

这道题的核心思路是:首先从每个箱子字符串中提取出所有的字母字符,忽略大小写并按升序排列,形成一个新的字符串。然后将这个新字符串与给定的密码进行比较,若完全一致,则认为该箱子匹配密码,返回其编号。如果没有匹配的箱子,则返回 -1。具体操作包括提取字母、转换大小写、排序,并通过逐个比较箱子中的密码与目标密码来实现匹配。

  1. 密码提取:
    首先,我们需要提取每个箱子字符串中的字母字符,并忽略其他字符(如数字、标点、空格等)。对提取出的字母,忽略大小写并按照升序排列。
  2. 匹配判断:
    如果提取出的字母字符串与给定的密码字符串完全一致(包括字母顺序和字符种类),则认为匹配成功,返回该箱子的编号。
  3. 性能考虑:
    题目中提到最多有 10000 个箱子,每个箱子的字符串最大长度为 50,所以我们可以在时间复杂度允许的范围内逐个检查每个箱子。
  4. 细节处理:
    忽略大小写的比较可以通过将所有字母转换为小写来实现。
    提取字母并排序后与密码直接比较。

python

def extract_and_sort(s):
    result = [c.lower() for c in s if c.isalpha()]
    return ''.join(sorted(result))

def find_matching_box(password, boxes):
    target_password = extract_and_sort(password)
    
    for i, box in enumerate(boxes):
        if extract_and_sort(box) == target_password:
            return i + 1  # 返回箱子编号,从1开始
    
    return -1

# 输入
password = input().strip()
boxes = input().strip().split()

# 输出匹配的箱子编号
print(find_matching_box(password, boxes))

java

import java.util.*;

public class Main {
    // 提取并排序字母
    public static String extractAndSort(String s) {
        StringBuilder result = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (Character.isLetter(c)) {
                result.append(Character.toLowerCase(c));
            }
        }
        char[] chars = result.toString().toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }

    public static int findMatchingBox(String password, String[] boxes) {
        String targetPassword = extractAndSort(password);
        
        for (int i = 0; i < boxes.length; i++) {
            if (extractAndSort(boxes[i]).equals(targetPassword)) {
                return i + 1;  // 返回箱子编号,从1开始
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String password = scanner.nextLine().trim();
        String[] boxes = scanner.nextLine().split(" ");
        
        // 输出匹配的箱子编号
        System.out.println(findMatchingBox(password, boxes));
    }
}

cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

// 提取并排序字母
string extract_and_sort(const string& s) {
    string result;
    for (char c : s) {
        if (isalpha(c)) {
            result.push_back(tolower(c));
        }
    }
    sort(result.begin(), result.end());
    return result;
}

int main() {
    string password;
    cin >> password;  // 密码字符串
    cin.ignore();  // 忽略换行符
    string boxes_line;
    getline(cin, boxes_line);  // 读取箱子的字符串

    vector<string> boxes;
    size_t pos = 0;
    // 将输入的多个箱子分割成字符串
    while ((pos = boxes_line.find(" ")) != string::npos) {
        boxes.push_back(boxes_line.substr(0, pos));
        boxes_line.erase(0, pos + 1);
    }
    boxes.push_back(boxes_line);

    // 对密码进行处理
    string target_password = extract_and_sort(password);

    // 检查每个箱子
    for (int i = 0; i < boxes.size(); ++i) {
        string box_password = extract_and_sort(boxes[i]);
        if (box_password == target_password) {
            cout << i + 1 << endl;  // 返回箱子编号,从1开始
            return 0;
        }
    }

    // 如果没有匹配的箱子
    cout << -1 << endl;
    return 0;
}


灵芸小骏
8.9k 声望845 粉丝

移动开发者。