首页 > 代码库 > Excuses, Excuses! UVA 409

Excuses, Excuses! UVA 409

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int get_word(int);
void convert_word();
int str_cmp();

char key[100][100];//保存关键词
char word[100];//保存从excuse中提取出来的词
char exc[100][100];//保存excuses
int  cnt[100];//记录每个excuse中keyword出现的次数
int max=0,j,K,E;

int main(){
 int i,N=1;
 //freopen("data","r",stdin);
 while(~scanf("%d%d",&K,&E)){
  for(i=0;i<K;i++)
   scanf("%s\n",key[i]);
  for(i=0;i<E;i++)
   gets(exc[i]);

  for(i=0;i<E;i++)
   cnt[i]=0;

  max=0;

  for(i=0;i<E;i++){
   j=0;//j为在读取excuse中单词时的标记,每新读一个excuse都要更新
   while(get_word(i)){//从excuse中提取单词
    convert_word();//将单词转化为小写字母
    if(str_cmp())//单词是否为keyword
     cnt[i]++;
    max=max>cnt[i]?max:cnt[i];
   }
  }  
  
  printf("Excuse Set #%d\n",N++);
  for(i=0;i<E;i++)
   if(cnt[i]==max)
    printf("%s\n",exc[i]);

   putchar('\n');

 }

return 0;
}

int get_word(int i){
 char c;
 int k=0;
 
 while(exc[i][j]!='\0'){
  while(!isalpha(exc[i][j])&&exc[i][j]!='\0')
  j++;

  if(exc[i][j]=='\0')
  return 0;
  
  word[k++]=exc[i][j++];
  while(isalpha(exc[i][j]))
  word[k++]=exc[i][j++];
 
  word[k]='\0';
  return 1;
 }

 return 0;
}

void convert_word(){
 int len=strlen(word),i;

 for(i=0;i<len;i++)
  if(isupper(word[i]))
   word[i]=tolower(word[i]);
 return ;
}

int str_cmp(){
 int i;

 for(i=0;i<K;i++)
  if(strcmp(key[i],word)==0)
   return 1;

 return 0;
}