UVA 409 (13.08.01)

 Excuses, Excuses! 

Judge Ito is having a problem with people subpoenaed for jury dutygiving rather lame excuses in order to avoid serving. In order toreduce the amount of time required listening to goofy excuses, Judge Itohas asked that you write a program that will search for a list ofkeywords in a list of excuses identifying lame excuses. Keywords can bematched in an excuse regardless of case.

Input

Input to your program will consist of multiple sets of data.

  • Line 1 ofeach set will contain exactly two integers. The first number ( tex2html_wrap_inline30 )defines the number of keywords to be used in the search. The secondnumber ( tex2html_wrap_inline32 ) defines the number of excuses in the set to besearched.
  • Lines 2 through K+1 each contain exactly one keyword.
  • LinesK+2 through K+1+E each contain exactly one excuse.
  • All keywords in thekeyword list will contain only contiguous lower case alphabeticcharacters of length L ( tex2html_wrap_inline42 ) and will occupy columns 1 throughL in the input line.
  • All excuses can contain any upper or lower casealphanumeric character, a space, or any of the following punctuationmarks [SPMamp".,!?&] not including the square brackets and will not exceed 70characters in length.
  • Excuses will contain at least 1 non-space character.

Output

For each input set, you are to print the worst excuse(s) from the list.

  • The worst excuse(s) is/are defined as the excuse(s) which contains thelargest number of incidences of keywords.
  • If a keyword occurs more thanonce in an excuse, each occurrance is considered a separate incidence.
  • A keyword ``occurs" in an excuse if and only if it exists in the stringin contiguous form and is delimited by the beginning or end of the lineor any non-alphabetic character or a space.

For each set of input, you are to print a single line with the number ofthe set immediately after the string ``Excuse Set #". (See the SampleOutput). 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 worstexcuse, you may print them in any order.

After each set of output, you should print a blank line.

Sample Input

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?

Sample Output

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!

题意: 给出k个单词, e行句子
对于每个句子, 看看里面有多少个单词是和前面给出的k个单词是一样的, 统计之
最后再比较一次, 哪个句子是出现上述单词数最多的, 打印出来
若有出现多个句子都是最多的, 按先后的顺序~

做法:
AC的代码里有注释, 应该能看清楚思路~
另外我用了三重的for循环, 代码不够优美~
将就着看, 以后再优化~

AC代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>

using namespace std;

char word[25][100];
char words[25][100];
int ans[25];

int main() {
	int k, e;
	int len, pos;
	int cas = 0;
	char ch, tmp[100];

	while(scanf("%d%d%c", &k, &e, &ch) != EOF) {
		for(int i = 0; i < k; i++)
			gets(word[i]);
		for(int i = 0; i < k; i++) {
			len = strlen(word[i]);
			for(int j = 0; j < len; j++)
				if(isupper(word[i][j]))
					word[i][j] += 32; //化为小写
		}
		for(int i = 0; i < e; i++)
			gets(words[i]);
		memset(ans, 0, sizeof(ans)); //每次都要清零, 后面用来检查哪句出现单词最多
		for(int i = 0; i < e; i++) { //针对每一句话进行搜索
			len = strlen(words[i]);
			pos = 0;
			for(int l = 0; l < len; l++) { //对当下这句话中的每个字符进行判断
				if(isupper(words[i][l]))
					tmp[pos++] = words[i][l] + 32;
				else if(islower(words[i][l]))
					tmp[pos++] = words[i][l];
				else { //这里是遇到不是字母的字符了, 说明当前这句找到一个完整单词
					tmp[pos] = '\0';
					for(int j = 0; j < k; j++) { //针对当前这句已搜到的单词, 与单词比, 一样的话, 单词数+1, 即ans数组++
						if(strcmp(tmp, word[j]) == 0)
							ans[i]++;
					}
					pos = 0;
				}
			}
		}
		printf("Excuse Set #%d\n", ++cas);
		int Max = 0;
		for(int i = 0; i < e; i++) {
			if(ans[i] > Max)
				Max = ans[i];
		}
		for(int i = 0; i < e; i++) {
			if(ans[i] == Max)
				puts(words[i]);
		}
		printf("\n"); //注意空行不漏~
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值