409 - Excuses, Excuses!

11 篇文章 0 订阅

题目:409 - Excuses, Excuses!


题目大意:找关键词出现最多的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  也可以自己写  。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值