返回目录

题目描述: 全量和已占用字符集 、字符串统计

给定两个字符集合,一个是全量字符集,一个是已占用字符集,已占用字符集中的字符不能再使用。
要求输出剩余可用字符集。

输入描述

  1. 输入一个字符串 一定包含@,@前为全量字符集 @后的为已占用字符集
  2. 已占用字符集中的字符一定是全量字符集中的字符
  3. 字符集中的字符跟字符之间使用英文逗号隔开
  4. 每个字符都表示为字符+数字的形式用英文冒号分隔,比如a:1标识一个a字符
  5. 字符只考虑英文字母,区分大小写
  6. 数字只考虑正整型 不超过100
  7. 如果一个字符都没被占用 @标识仍存在,例如 a:3,b:5,c:2@

输出描述

  • 输出可用字符集
  • 不同的输出字符集之间用回车换行
  • 注意 输出的字符顺序要跟输入的一致,如下面用例不能输出b:3,a:2,c:2
  • 如果某个字符已全部占用 则不需要再输出

示例:

输入a:3,b:5,c:2@a:1,b:2
输出a:2,b:3,c:2
说明全量字符集为三个a,5个b,2个c
已占用字符集为1个a,2个b
由于已占用字符不能再使用
因此剩余可用字符为2个a,3个b,2个c
因此输出a:2,b:3,c:2

Python算法源码

def main():
    input_str = input()

    # 将输入字符串按照@符号分割为全量字符集和已占用字符集
    full_character_set, occupied_character_set = input_str.split("@")

    # 创建字符列表,用于存储全量字符集中的字符及其对应的数量
    character_list = []

    # 将全量字符集按照逗号分割为单个字符
    full_character_set_list = full_character_set.split(",")

    # 遍历全量字符集的每个字符
    for character in full_character_set_list:
        # 将字符按照冒号分割为字符和数量
        character_split = character.split(":")
        character_list.append((character_split[0], int(character_split[1]))) # 将字符和数量添加到字符列表中

    # 如果已占用字符集为空,则输出全量字符集
    if occupied_character_set == "":
        print(full_character_set + "@")
        return

    # 创建已占用字符集的字典,用于存储已占用字符及其对应的数量
    occupied_characters = {}

    # 将已占用字符集按照逗号分割为单个字符
    occupied_character_set_list = occupied_character_set.split(",")

    # 遍历已占用字符集的每个字符
    for character in occupied_character_set_list:
        # 将字符按照冒号分割为字符和数量
        character_split = character.split(":")
        occupied_characters[character_split[0]] = int(character_split[1]) # 将字符和数量添加到已占用字符集的字典中

    # 遍历字符列表中的每个字符
    i = 0
    while i < len(character_list):
        character, count = character_list[i]
        # 如果已占用字符集中包含当前字符
        if character in occupied_characters:
            count -= occupied_characters[character] # 计算剩余可用数量
            if count > 0:
                character_list[i] = (character, count) # 更新字符列表中的数量为剩余可用数量
            else:
                del character_list[i] # 如果剩余可用数量为0,则移除当前字符
                i -= 1 # 由于移除了一个字符,需要将索引减1
        i += 1

    # 构建输出字符串
    result = ",".join([f"{char}:{count}" for char, count in character_list])
    print(result)

if __name__ == "__main__":
    main()

C算法源码


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char input_str[1000];
    fgets(input_str, sizeof(input_str), stdin);
    input_str[strcspn(input_str, "\n")] = 0; // 移除换行符

    // 将输入字符串分割为完整字符集和占用字符集
    char *full_character_set = strtok(input_str, "@");
    char *occupied_character_set = strtok(NULL, "@");

    // 创建字符列表以存储字符及其计数
    typedef struct {
        char character;
        int count;
    } CharacterCount;
  
    CharacterCount character_list[100]; // 假设最多100个字符
    int list_size = 0;

    // 将完整字符集分割为单个字符
    char *token = strtok(full_character_set, ",");
    while (token != NULL) {
        char character;
        int count;
        sscanf(token, "%c:%d", &character, &count);
        character_list[list_size].character = character;
        character_list[list_size].count = count;
        list_size++;
        token = strtok(NULL, ",");
    }

    // 如果占用字符集为空,则输出完整字符集
    if (occupied_character_set[0] == '\0') {
        printf("%s@\n", full_character_set);
        return 0;
    }

    // 创建占用字符的哈希表
    typedef struct {
        char character;
        int count;
    } OccupiedCharacter;
  
    OccupiedCharacter occupied_characters[100]; // 假设最多100个占用字符
    int occupied_size = 0;

    // 将占用字符集分割为单个字符
    token = strtok(occupied_character_set, ",");
    while (token != NULL) {
        char character;
        int count;
        sscanf(token, "%c:%d", &character, &count);
        occupied_characters[occupied_size].character = character;
        occupied_characters[occupied_size].count = count;
        occupied_size++;
        token = strtok(NULL, ",");
    }

    // 遍历字符列表并根据占用字符更新计数
    for (int i = 0; i < list_size; i++) {
        for (int j = 0; j < occupied_size; j++) {
            if (character_list[i].character == occupied_characters[j].character) {
                character_list[i].count -= occupied_characters[j].count;
                break;
            }
        }
    }

    // 构造输出字符串
    char result[1000] = "";
    for (int i = 0; i < list_size; i++) {
        char temp[50];
        sprintf(temp, "%c:%d,", character_list[i].character, character_list[i].count);
        strcat(result, temp);
    }

    // 从结果中移除最后一个逗号
    result[strlen(result) - 1] = '\0';

    printf("%s\n", result);

    return 0;
}

Java算法源码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String inputStr = scanner.nextLine();

        // 将输入字符串分割为完整字符集和占用字符集
        String[] sets = inputStr.split("@");
        String fullCharacterSet = sets[0];
        String occupiedCharacterSet = sets[1];

        // 分割完整字符集为单个字符
        String[] fullCharacters = fullCharacterSet.split(",");
        CharacterCount[] characterList = new CharacterCount[100]; // 假设最多100个字符
        int listSize = 0;

        for (String fullCharacter : fullCharacters) {
            String[] parts = fullCharacter.split(":");
            char character = parts[0].charAt(0);
            int count = Integer.parseInt(parts[1]);
            characterList[listSize] = new CharacterCount(character, count);
            listSize++;
        }

        // 如果占用字符集为空,则输出完整字符集
        if (occupiedCharacterSet.isEmpty()) {
            System.out.println(fullCharacterSet + "@");
            return;
        }

        // 分割占用字符集为单个字符
        String[] occupiedCharacters = occupiedCharacterSet.split(",");
        OccupiedCharacter[] occupiedCharacterList = new OccupiedCharacter[100]; // 假设最多100个占用字符
        int occupiedSize = 0;

        for (String occupiedCharacter : occupiedCharacters) {
            String[] parts = occupiedCharacter.split(":");
            char character = parts[0].charAt(0);
            int count = Integer.parseInt(parts[1]);
            occupiedCharacterList[occupiedSize] = new OccupiedCharacter(character, count);
            occupiedSize++;
        }

        // 遍历字符列表并根据占用字符更新计数
        for (int i = 0; i < listSize; i++) {
            for (int j = 0; j < occupiedSize; j++) {
                if (characterList[i].character == occupiedCharacterList[j].character) {
                    characterList[i].count -= occupiedCharacterList[j].count;
                    break;
                }
            }
        }

        // 构造输出字符串
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < listSize; i++) {
            result.append(characterList[i].character).append(":").append(characterList[i].count).append(",");
        }

        // 从结果中移除最后一个逗号
        result.deleteCharAt(result.length() - 1);

        System.out.println(result.toString());
    }

    static class CharacterCount {
        char character;
        int count;

        public CharacterCount(char character, int count) {
            this.character = character;
            this.count = count;
        }
    }

    static class OccupiedCharacter {
        char character;
        int count;

        public OccupiedCharacter(char character, int count) {
            this.character = character;
            this.count = count;
        }
    }
}
最后修改:2024 年 04 月 03 日
如果觉得我的文章对你有用,请随意赞赏