UVA 156 (暑假-排序、检索(2)-A-Ananagrams)

#include <stdlib.h>
#include <cstring>
#include <iostream>
using namespace std;

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

int cmp_string(const void *_a, const void *_b)
{
	return strcmp((char*)_a, (char*)_b);
}

int main() {
	char word[1050][25], sorted[1050][25];

	//输入数据
	int count = 0;
	while (cin>> word[count]) {
		if (word[count][0] == '#')
			break;
		count++;		
	}
	
	//字符串排序
	qsort(word, count, sizeof(word[0]), cmp_string);

	//全部字符转换为小写
	for (int i = 0; i < count; i++) {	
		strcpy(sorted[i], word[i]);
	   	for (int j = 0; j < strlen(word[i]); j++)		 
			if (sorted[i][j] >= 'A' && sorted[i][j] <= 'Z')
				 sorted[i][j] += 32;
	}

	//每个单词的字符排序
	for (int i = 0; i < count; i++) 
		qsort(sorted[i], strlen(sorted[i]), sizeof(char), cmp_char);

	//输出不是相同的单词
	for (int i = 0; i < count; i++) {
		bool kong = true;
		for (int j = 0; j < count; j++)
			if (!strcmp(sorted[i], sorted[j]) && i != j) {
				kong = false;
				break;
			}
		if (kong)
			cout<< word[i]<< endl;
	}
	return 0;
}

你可能感兴趣的:(UVA 156 (暑假-排序、检索(2)-A-Ananagrams))