UVa-409-Excuses, Excuses!

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving ::String


// 409 - Excuses, Excuses!
#include <cstdio>
#include <cstring>
#include <cctype>

int main(void)
{
	char *found, p, q;
	int i, j, k, e, c, n, len, max, cnt=1, times[20];
	char keyword[20][20], excuse[20][80], excuse_t[20][80];
	while(scanf("%d%d", &k, &e) != EOF)
	{
		for(i = 0; i < k; i++)
			scanf("%s", keyword[i]);
		getchar();
		for(i = 0; i < e; i++)
		{
			n = 0;
			while((c=getchar())!='\n' && c!=EOF)
			{
				excuse[i][n] = c;
				excuse_t[i][n] = tolower(c);
				n++;
			}
			excuse[i][n] = '\0';
			excuse_t[i][n] = '\0';
		}

		memset(times, 0, sizeof(times));
		for(i = 0; i < e; i++)
		{
			for(j = 0; j < k; j++)
			{
				len = strlen(keyword[j]);
				found = strstr(excuse_t[i], keyword[j]);
				while(found != NULL)
				{
					if(found == excuse_t[i])
					{
						sscanf(found+len, "%c", &q);
						if(!isalpha(q))
							times[i]++;
					}
					else
					{
						sscanf(found-1, "%c", &p);
						sscanf(found+len, "%c", &q);
						if(!isalpha(p) && !isalpha(q))
							times[i]++;
					}
					found = strstr(found+len, keyword[j]);
				}
			}
		}

		max = -1;
		for(i = 0; i < e; i++)
			if(times[i] > max)
				max = times[i];
		printf("Excuse Set #%d\n", cnt++);
		for(i = 0; i < e; i++)
			if(times[i] == max)
				printf("%s\n", excuse[i]);
		printf("\n");
	}

	return 0;
}


posted @ 2014-09-28 15:42  颜威  阅读(189)  评论(0编辑  收藏  举报