UVa 409 - Excuses, Excuses!
写的很繁琐。
1 # include <stdio.h>
2 # include <ctype.h>
3
4 # define MAX_WORD_LEN 25
5 # define MAX_LINE_LEN 75
6 # define MAXN 25
7
8 char keyw[MAXN][MAX_WORD_LEN];
9 char line[MAXN][MAX_LINE_LEN];
10 char copy[MAX_LINE_LEN];
11 int cnt[MAXN];
12
13 int key_cnt(char *line, int len, char *keyword);
14
15 int main()
16 {
17 int i, j, k, e, len, max, T;
18
19 T = 0;
20 while (~scanf("%d%d", &k, &e))
21 {
22 ++T;
23 getchar();
24 for (i = 0; i < k; ++i) gets(keyw[i]);
25 for (i = 0; i < e; ++i) gets(line[i]);
26 for (max = i = 0; i < e; ++i)
27 {
28 cnt[i] = 0;
29 len = strlen(line[i]);
30 strcpy(copy, line[i]);
31 for (j = 0; j < len; ++j)
32 if (isalpha(copy[j])) copy[j] = tolower(copy[j]);
33 for (j = 0; j < k; ++j)
34 cnt[i] += key_cnt(copy, len, keyw[j]);
35 if (cnt[i] > max) max = cnt[i];
36 }
37 printf("Excuse Set #%d\n", T);
38 for (i = 0; i < e; ++i)
39 if (cnt[i] == max) puts(line[i]);
40 printf("\n", max);
41 memset(cnt, 0, sizeof(cnt));
42 }
43
44 return 0;
45 }
46
47 int key_cnt(char *line , int len, char *keyword)
48 {
49 int cnt, lenKey;
50 char *p, *tmp;
51
52 cnt = 0;
53 lenKey = strlen(keyword);
54 p = line;
55 while (p-line < len)
56 {
57 tmp = strstr(p, keyword);
58 if (tmp == NULL) break;
59 else if (tmp==line || tmp+lenKey==line+len-1 || (!isalpha(*(tmp-1))&&!isalpha(*(tmp+lenKey)))) ++cnt;
60 p = tmp + lenKey;
61 }
62 return cnt;
63 }
/* */