2013年8月19日

UVa 409 - Excuses, Excuses!

#include <cstdio>
#include <cctype>
#include <string>
#include <sstream>
using namespace std;

int main() {
  char s[999];
  int C = 1;
  while (gets(s)) {
    int K, E;
    sscanf(s, "%d%d", &K, &E);
    char keywords[20][99];
    for (int i = 0; i < K; i++) {
      gets(keywords[i]);
    }
    int num[20] = {}, max = 0;
    char excuses[20][99];
    for (int i = 0; i < E; i++) {
      gets(excuses[i]);
      string sentence(excuses[i]);
      for (int j = 0; sentence[j]; j++) {
        if (!isalpha(sentence[j])) {
          sentence[j] = ' ';
        } else {
          sentence[j] = tolower(sentence[j]);
        }
      }
      istringstream ss(sentence);
      string word;
      while (ss >> word) {
        for (int j = 0; j < K; j++) {
          num[i] += (word == keywords[j]);
        }
      }
      max = num[i] > max ? num[i] : max;
    }
    printf("Excuse Set #%d\n", C++);
    for (int i = 0; i < E; i++) {
      if (num[i] == max) {
        puts(excuses[i]);
      }
    }
    puts("");
  }
  return 0;
}

沒有留言:

張貼留言