UVaOJ 409 - Excuses, Excuses!

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: String


Description

无论在公司,还在学校。

为迟早找借口的事常有发生。

你要做的就是,给你一些借口中常常含有的关键词。

找出含有关键词数最多的借口。

注意一下几点:

  • 关键词以小写形式给出,但在借口中,不计大小写。
  • 即使一个借口中有多个关键词相同,它们仍然计算在关键词数内。
  • 关键词在借口中应该连续,且前后必须有“换行”、“空格”或者“非字母字符”。


Type

String


Analysis

将关键词们放在一个set中,

找出每个借口中的单词,判断是否为关键词即可。


Solution

// UVaOJ 409
// Excuses, Excuses!
// by A Code Rabbit

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;

const int MAXN = 22;

int k, e;
string str;
string excuses[MAXN];
int cnt[MAXN];

int main() {
    int cnt_case = 0;
    while (cin >> k >> e) {
        // Input and solve.
        cin.get();
        set<string> keywords;
        for (int i = 0; i < k; i++) {
            getline(cin, str);
            keywords.insert(str);
        }
        memset(cnt, 0, sizeof(cnt));
        int max_cnt = 0;
        for (int i = 0; i < e; i++) {
            getline(cin, excuses[i]);
            str = excuses[i];
            string tmp;
            for (int j = 0; j < str.length(); j++) {
                if (!isalpha(str[j])) {
                    if (keywords.count(tmp)) cnt[i]++;
                    tmp = "";
                } else {
                    tmp += tolower(str[j]);
                }
            }
            max_cnt = max(max_cnt, cnt[i]);
        }
        // Output.
        cout << "Excuse Set #" << ++cnt_case << endl;
        for (int i = 0; i < e; i++)
            if (cnt[i] == max_cnt)
                cout << excuses[i] << endl;
        cout << endl;
    }

    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值