[code] PTA 胡凡算法筆記 DAY023

題目 A1084

在這裏插入圖片描述

  • 題意
    根據輸入的兩個字符串判斷輸出壞掉的鍵,不用重複輸出,字母用大寫輸出。

  • 思路
    用原始串掃描輸出串,不相同的字符即爲壞掉的鍵,輸出時需判斷之前是否已記錄過,這裏我採取map的方法做映射。
    注意:可能掃描完了輸出串,原始串後部分還未判斷。

  • Code in C++

#include <cctype>
#include <cstdio>
#include <map>
#define maxn 85
int main()
{
    char origin[maxn], out[maxn];
    scanf("%s", origin);
    scanf("%s", out);
    std::map<char, int> worn;
    int i, j;
    for (i = 0, j = 0; origin[i] != '\0' && out[j] != '\0'; ++i) {
        if (origin[i] != out[j]) {
            char tmp = origin[i];
            if (isalpha(tmp)) {
                tmp = toupper (tmp);
            }
            if (worn.count(tmp)== 0) {
                printf("%c", tmp);
                worn[tmp] = 1;
            }
        } else {
            ++j;
        }
    }

    // 判斷原串最後一位
    for (; origin[i] != '\0'; ++i) {
        char tmp = origin[i];
        if (isalpha(tmp)) {
            tmp = toupper (tmp);
        }
        if (worn.count(tmp)== 0) {
            printf("%c", tmp);
            worn[tmp] = 1;
        }
    }
    return 0;
}


題目 B1033 舊鍵盤打字

在這裏插入圖片描述

  • 題意
    給出壞掉的鍵和原始串,輸出顯示的串。

  • 思路
    這裏其實主要就是記住壞掉的鍵和關於大寫字母輸出的邏輯即可。壞掉鍵中的大寫字母都換爲小寫字母(不換將輸入串的小寫變大寫再判斷也可以)。
    注意:壞掉的鍵可能輸入很長也可能爲空,也需要用長串變量接收,採取getline方式。最後需要輸出空行。

  • Code in C++

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
bool hashTable[256];
std::string str;
int main()
{
    memset(hashTable, true, sizeof(hashTable));
    getline(std::cin, str);
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] >= 'A' && str[i] <= 'Z')
            str[i] = str[i] - 'A' + 'a';
        hashTable[str[i]] = false;
    }
    getline(std::cin, str);
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            char tmp = str[i] - 'A' + 'a';
            if (hashTable[tmp] == true && hashTable['+'] == true ) {
                printf("%c", str[i]);
            }
        } else if (hashTable[str[i]] == true){
                printf("%c", str[i]);
        }
    }
    printf("\n");
    return 0;
}


小結

字符串的輸入方式還是沒有掌握地很好。然後就是題目中的一些細節沒有掌握還有一些邏輯寫錯了地方導致debug了很久。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章