天天看點

1092. To Buy or Not to Buy (20)解題報告

本代碼采用了散列的方法。

#include <iostream>
#include <cstdio>
#include <cstdlib>

void hash(int arr[], char str[]);

int main(int argc, char** argv) {
	int origin[100] = {0}, want[100] = {0}, i, left = 0, more = 0;
	char str1[1010], str2[1010];
	bool flag;
	scanf("%s", str1);
	scanf("%s", str2);
	hash(origin, str1);
	hash(want, str2);
	flag = true;
	for(i = 0; i < 100; i++){
		if(want[i] > origin[i]){
			flag = false;
			left += want[i] - origin[i];
		}
		else{
			more += origin[i] - want[i];
		}
	}
	if(flag){
		printf("%s %d", "Yes", more);
	}
	else{
		printf("%s %d", "No", left);
	}
	return 0;
}

void hash(int arr[], char str[]){
	int i;
	for(i = 0; str[i] != '\0'; i++){
		if(str[i] >= '0' && str[i] <= '9'){
			arr[str[i] - '0']++;
		}
		else if(str[i] >= 'a' && str[i] <= 'z'){
			arr[str[i] - 'a' + 10]++;
		}
		else{
			arr[str[i] - 'A' + 36]++;
		}
	}
	return;
}
           

繼續閱讀