Excuses, Excuses! |
Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.
Input to your program will consist of multiple sets of data.
SPMamp
".,!?&] not including the square brackets and will not exceed 70 characters in length.For each input set, you are to print the worst excuse(s) from the list.
For each set of input, you are to print a single line with the number of the set immediately after the string ``Excuse Set #". (See the Sample Output). The following line(s) is/are to contain the worst excuse(s) one per line exactly as read in. If there is more than one worst excuse, you may print them in any order.
After each set of output, you should print a blank line.
5 3 dog ate homework canary died My dog ate my homework. Can you believe my dog died after eating my canary... AND MY HOMEWORK? This excuse is so good that it contain 0 keywords. 6 5 superhighway crazy thermonuclear bedroom war building I am having a superhighway built in my bedroom. I am actually crazy. 1234567890.....,,,,,0987654321?????!!!!!! There was a thermonuclear war! I ate my dog, my canary, and my homework ... note outdated keywords?
Excuse Set #1 Can you believe my dog died after eating my canary... AND MY HOMEWORK? Excuse Set #2 I am having a superhighway built in my bedroom. There was a thermonuclear war!题目大意:给出一些借口,再给出一些句子。输出包含最多借口的句子。
注意:有1个以上并列最多时,按输入顺序输出。
借口不能为句子中单词的子串,这样时非法的,例如:
1 2
cao
wo cao
wocao
输出:wo cao
解题思路:借用strstr,判断时要判断strstr返回的地址的后k(当前借口的长度)位是否为字母,为字母的不合法。
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define N 111 char str[N][N]; char tem[N][N]; char pop[N][N]; int num[N]; void strdx(char f[]) { int k = strlen(f); for (int i = 0; i < k; i++) if (f[i] >= 'A' && f[i] <= 'Z') f[i] +=32; } int main() { int n, m; int t = 1; while (cin >> n >> m) { getchar(); // Init. memset(str, 0, sizeof(str)); memset(tem, 0, sizeof(tem)); memset(num, 0, sizeof(num)); memset(pop, 0, sizeof(pop)); // Read. for (int i = 0; i < n; i++) { gets(str[i]); strdx(str[i]); } for (int i = 0; i < m; i++) { gets(tem[i]); strcpy(pop[i], tem[i]); strdx(pop[i]); } // Count. for (int i = 0; i < m; i++) { char *move = NULL; for (int j = 0; j < n; j++) { move = strstr(pop[i], str[j]); if (move == NULL) continue; int k = strlen(str[j]); if (*(move + k) >= 'a' && *(move + k) <= 'z') continue; num[i]++; } } int max = 0; for (int i = 0; i < m; i++) if (num[i] > max) max = num[i]; cout << "Excuse Set #" << t++ << endl; for (int i = 0; i < m; i++) if (max == num[i]) puts(tem[i]); cout << endl; } return 0; }