UVA 10420 (暑假-排序、检索 -B - List of Conquests)

#include 
#include 
//#include 
#include 
using namespace std;

int  strcompare(const void *_a, const void *_b)
{
	char *a = (char*)_a;
	char *b = (char*)_b;
	return strcmp(a, b);
}

int main() {
	const int Max = 1050;
	int n;
	while (scanf("%d", &n) != EOF) {
		char str_1[2 * Max][100], str_2[2 * Max][100];
		getchar();
		
		char str[Max];  

		for (int i = 0; i < n; i++) {
			memset(str, 0, sizeof(str));
			gets(str);							//保存输入的数据;
			int len = strlen(str), kong = 0, k = 0;

			//截取国家名
			for (int j = 0; j < len; j++) {      
				if (str[j] >= 'a' && str[j] <= 'z' || str[j] >= 'A' && str[j] <= 'Z')
					str_1[i][k++] = str[j], kong = 1;
				else if (kong) {
					str_1[i][k] = 0;
					break;
				}
			}
		}
		

		
		//按字母表顺序排序
//		for (int i = 0; i < n; i++)
			qsort(str_1, n, sizeof(str_1[0]), strcompare);
	/*	for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				if (strcmp(str_1[i], str_1[j]) > 0) 
				{
					char s[100];	
					strcpy(s, str_1[i]);
					strcpy(str_1[i], str_1[j]);
					strcpy(str_1[j], s);
				}
			}
		}
	*/
		

					//计数并输出
		int count = 1;
		for (int i = 0; i < n; i++)
			if (strcmp(str_1[i], str_1[i+1]) != 0)
				printf("%s %d\n", str_1[i], count), count = 1;
			else
				count++;
	}
	return 0;
}

你可能感兴趣的:(排序与检索,ACM,刷题录)