嗜血魂K

导航

409 Excuses, Excuses!

思路:读入excuses之后,预处理为小写,再利用strstr()函数.
strstr(char* s1,char* s2) 藐视是返回s2再s1中第一次出现的位置,若找不到,则返回NULL.
由于只能是第一次,这里就有点麻烦了,我思考了一会,想到以前看地很渣的指针的知识,用一个char*指针来存放s1首元素位置,每找到一个keyword,就位移sizeof(char)*strlen(keyword)长度.(= =鉴于表述能力和基础不扎实)
e.g s1:abcdefg s2:abc
............还是很纠结= =            这样吧
char *c = s1; printf("%s", c)  ->abcdefg
c + strlen(s2) -> c指向d的位置.   printf("%s", c) -> defg;
//应该是这样的吧.基础太渣唉
--------------------------------------------------------------------------------------------------------------------------------
解决这个关键问题后,又被一些字符串处理的小问题困扰着,比如用fgets前要getchar读掉换行啊= =
还有储存新字符串前,要用memset清一下. 
其他就记不清了.
总之...熟练问题吧,毕竟为语法那边太水了.
//这道题藐视比较简单,用strstr函数处理应该就可以了
//为使strstr函数比较,在每次读入excuses时,应预处理为小写.
#include<stdio.h>
#include
<string.h>
#include
<ctype.h>
int main()
{
#ifdef LOCAL
freopen(
"input.txt", "r", stdin);
#endif
int k, e, count[20], max, len;
int i, j, n = 1;
char s_k[20][30], s_e[20][80], buf[80];
while(scanf("%d%d", &k, &e) == 2)
{
max
= 0;
memset(count,
0, 30);
for(i = 0; i < k; i++) scanf("%s", s_k[i]);
getchar();
//因为用fgets读
for(i = 0; i < e; i++)
{
memset(buf,
0, 80);
fgets(s_e[i],
80, stdin);
for(j = 0, len = strlen(s_e[i]); j < len; j++) buf[j] = tolower(s_e[i][j]);
for(j = 0; j < k; j++)
{
char *c = buf;
while((c = strstr(c, s_k[j])) != NULL)
{
count[i]
++;
c
+= strlen(buf);
}
}
if(count[i] > max) max = count[i];
}

printf(
"Excuse Set #%d\n", n++);
for(i = 0; i < e; i++)
if(count[i] == max) printf("%s", s_e[i]);
printf(
"\n");
}
return 0;
}

  

posted on 2011-07-22 19:39  嗜血魂K  阅读(342)  评论(2)    收藏  举报