题目大意:给定几个keywords,再给几个excuses,要求从中找出keywords最多的excuse。如果有几个excuse一样多,就都输出来。其中要求出现的keyword是有条件的:
- If a keyword occurs more than once in an excuse, each occurrance is considered a separate incidence.
- A keyword ``occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non-alphabetic character or a space.
所以其实就是在句子里面找单词。这个单词可以夹在非字母字符的中间,如:123what234。所以可以在句子里面搜索一个个的单词再去进行匹配。
#include<stdio.h> #include<string.h> #include<ctype.h> char keywords[105][105]; char excuses[106][105],change[106][105]; int K,E; int search(int t) { int l=strlen(change[t]); char s[105]; int j=0,k,num=0; for(int i=0;i<l;i++) { if(isalpha(change[t][i]))s[j++]=change[t][i]; //找到一个连续的单词,因为不连续的是不符合的。 else { s[j]='\0'; j=0; for(k=0;k<K;k++) if(strcmp(s,keywords[k])==0)num++; } } return num; } int main() { int i,j,k,l,t[105],maxi,cases=0; while(scanf("%d%d",&K,&E)!=EOF) { maxi=0;cases++; memset(t,0,sizeof(t)); for(i=0;i<K;i++) { scanf("%s",keywords[i]); } getchar(); for(i=0;i<E;i++) { t[i]=0; gets(excuses[i]); for(k=0;k<strlen(excuses[i]);k++) {if(excuses[i][k]>='A'&&excuses[i][k]<='Z') change[i][k]=excuses[i][k]+32; else change[i][k]=excuses[i][k];} t[i]=search(i); if(t[i]>maxi)maxi=t[i]; } printf("Excuse Set #%d\n",cases); for(i=0;i<E;i++){ // printf("%s\n",change[i]); // printf("%d\n",t[i]); if(maxi==t[i])printf("%s\n",excuses[i]); } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-24 17:17:13