【題解】ZeroJudge c114: 00409 – Excuses, Excuses!

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c114
【解題想法】

  • 關鍵字只包含連續的小寫字母。
  • 測資可能包含像 “Guilty#reststop ate#superhighway#bedroom#thermonuclear@guilty!” 這樣的藉口。
  • 利用非英文字母的字元進行斷字。
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int K, E, Case = 1;
    while (cin >> K >> E){
        string keyword[K];
        for (int i = 0; i < K; i++){
            cin >> keyword[i];
        }
        string s;
        getline(cin, s);
        string excuse[E];
        int cnt[20] = {0};
        int mx = 0;
        for (int i = 0; i < E; i++){
            getline(cin, s);
            excuse[i] = s;
            int idx = 0;
            string word = "";
            while (idx < s.size()){
                char c = tolower(s[idx]);
                if (c >= 'a' && c <= 'z') {
                    word += c;
                } else {
                    for (int k = 0; k < K; k++){
                        if (word == keyword[k]) {
                            cnt[i]++;
                            break;
                        }
                    }
                    word = "";
                }
                idx++;
            }
            mx = max(mx, cnt[i]);
        }
        cout << "Excuse Set #" << Case++ << "\n";
        for (int i = 0; i < E; i++) {
            if (cnt[i] == mx){
                cout << excuse[i] << "\n";
            }
        }
        cout << "\n";
    }
    return 0;
}
分享本文 Share with friends