题目大意:找关键词出现最多的excuse;如果一样多就将excuse按顺序输出;
解题思路:每个关键词都在每个句子中搜索一遍,找到就在句子的count【i】++;注意句子中和关键字一样的字符串前后必须不是字母,例如关键字是key 句子:my keyjie 这样是不对的不算出现。
第一种:
#include<stdio.h>
#include<string.h>
const int N = 25;
const int M = 75;
int k, e, t = 0 ;
char str1[N][N];
char str2[N][M];
char str3[N][M];
int count[N];
bool search(int x ,int y, int z) {
int i, j;
for (i = 1, j = y+1; i < strlen(str1[z]); i++, j++) {
/*printf("%d\n",strlen(str1[z]));*/
if (j < 0 || j >= strlen(str3[x])) {
return false;
}
if (str3[x][j] != str1[z][i]) {
return false;
}
}
return true;
}
void find(int x) {
int i, j, len;
for (i = 0; i< e; i++) {
for (j = 0; j < strlen(str3[i]); j++) {
if (str1[x][0] == str3[i][j]) {
if(search(i, j, x)) {
len = strlen(str1[x]);
if(str3[i][j+len] < 'a' || str3[i][j+len] > 'z') {
count[i]++;
j += len;
continue;
}
}
}
}
}
}
void change() {
int i, j;
for( i = 0; i < e; i++)
for (j = 0; j < strlen(str2[i]); j++)
str3[i][j] = str2[i][j];
for (i = 0; i < e; i++){
for (j = 0; j < strlen(str3[i]); j++) {
if (str3[i][j] >= 'A' && str3[i][j] <= 'Z')
str3[i][j] += 32;
}
/* puts(str3[i]);*/
}
}
int max() {
int m = count[0];
for (int i = 1; i < k; i++)
m = (m > count[i])? m: count[i];
return m;
}
int main() {
while(scanf("%d %d%*c", &k, &e)==2) {
t++;
printf("Excuse Set #%d\n", t);
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
memset(str3, 0, sizeof(str3));
memset(count, 0, sizeof(count));
int i;
for( i = 0; i < k; i++)
gets(str1[i]);
for (i = 0; i < e; i++)
gets(str2[i]);
change();
for (i = 0; i < k; i++)
find(i);
//for (i = 0; i < k; i++)
// printf("%d",count[i]);
for (i = 0; i < k; i++)
if (count[i] == max())
puts(str2[i]);
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
const int N = 25;
const int M = 75;
int k, e, t = 0 ;
char str1[N][N];
char str2[N][M];
char str3[N][M];
int count[N];
void find(int x) {
int i, j, len;
char *move;
for (i = 0; i< e; i++) {
move = strstr(str3[i],str1[x]);
len = strlen(str1[x]);
if (move != NULL && (*(move+len) < 'a' || *(move+len) > 'z')) {
count[i]++;
continue;
}
}
}
void change() {
int i, j;
for( i = 0; i < e; i++)
for (j = 0; j < strlen(str2[i]); j++)
str3[i][j] = str2[i][j];
for (i = 0; i < e; i++){
for (j = 0; j < strlen(str3[i]); j++) {
if (str3[i][j] >= 'A' && str3[i][j] <= 'Z')
str3[i][j] += 32;
}
}
}
int max() {
int m = count[0];
for (int i = 1; i < k; i++)
m = (m > count[i])? m: count[i];
return m;
}
int main() {
while(scanf("%d %d%*c", &k, &e)==2) {
t++;
printf("Excuse Set #%d\n", t);
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
memset(str3, 0, sizeof(str3));
memset(count, 0, sizeof(count));
int i;
for( i = 0; i < k; i++)
gets(str1[i]);
for (i = 0; i < e; i++)
gets(str2[i]);
change();
for (i = 0; i < k; i++)
find(i);
for (i = 0; i < k; i++)
if (count[i] == max())
puts(str2[i]);
printf("\n");
}
return 0;
}
寻找可以用strstr 头文件string.h 也可以自己写 。