UVa 409 - Excuses, Excuses!

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

有點麻煩。
先把讀入的字串,分成一個一個 word ,分成 word 後再去一個一個對照有沒有相符的 key。
注意 word 的定義,要由非字母字元來區隔。

Code

UVa 409
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<cstdio>
#include<cctype>
#include<cstring>

char key[20][30];
inline int check(char word[], int K)
{
int j, count = 0;
for (j = 0; j < K; j++)//key
{
if (!strcmp(key[j], word))
{
count++;
break;
}
}

return count;
}
int main()
{
int K, E, Case = 1;
char excuse[20][75], str[75];
while (scanf("%d%d%*c", &K, &E) != EOF)
{
int i, j, count[20] = {};
char word[30];
for (i = 0; i < K; i++)
gets(key[i]);

for (i = 0; i < E; i++)
{
gets(excuse[i]);
for (j = 0; excuse[i][j]; j++)
if (!isalpha(excuse[i][j]))
str[j] = ' ';
else
str[j] = tolower(excuse[i][j]);
excuse[i][j] = str[j] = NULL;

bool flag = false;
int len = strlen(str), idx = 0;
for (int s = 0; s < len; s++)
{
if (str[s] != ' ')
{
word[idx++] = str[s];
if (!flag)
flag = true;
}
else if (flag)
{
flag = false;
word[idx] = NULL;
count[i] += check(word, K);
idx = 0;
}
}
if (str[len - 1] != ' ')//最後一個word還沒算到
{
word[idx] = NULL;
count[i] += check(word, K);
}
}

int max = 0;
for (i = 0; i < E; i++)
if (max < count[i])
max = count[i];

printf("Excuse Set #%d\n", Case++);
for (i = 0; i < E; i++)
if (count[i] == max)
puts(excuse[i]);
putchar('\n');
}

return 0;
}