0
点赞
收藏
分享

微信扫一扫

UVA - 156 Ananagrams


题目大意:把所有字母都相同的单词删除掉,字母不区分大小写的,然后进行排序,但输出时要原样输出

解题思路:简单的排序和转换

#include<cstdio>
#include<cstring>

int main() {
	char str[100000];
	char word[1000][100];
	char temp[1000][100];
	char s[1000];
	int mark[1000];
	char last[100][100];
	
	memset(str,'\0',sizeof(str));
	memset(word,'\0',sizeof(word));
	memset(temp,'\0',sizeof(temp));
	memset(s,'\0',sizeof(s));
	memset(mark,1,sizeof(mark));
	while(gets(s)) {
		if(strlen(s) == 1 && s[0] == '#' )
			break;
		strcat(str,s);	
		str[strlen(str)] = '\n';
	}

	int count = 0;
	int len = strlen(str);
	for(int i = 0; i < len; i++)//提取单词 
		if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
			for(int j = 0;;)
				if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
					word[count][j] = str[i];	
					temp[count][j] = str[i];
					i++;
					j++;
				}
				else {
					count++;
					break;
				}

	for(int i = 0; i < count ; i++) {
		int len = strlen(temp[i]);
		for(int j = 0; j < len; j++)//转换大小写
			if(temp[i][j] >= 'A' && temp[i][j] <= 'Z')
				temp[i][j] = temp[i][j] + 32;
	}
	/*for(int i = 0; i < count ; i++)
	  printf("%s\n", temp[i]);
	  */
	char t;
	for(int i = 0; i < count; i++) {//对每个单词的字母进行排序
		int len = strlen(temp[i]);
		for(int j = 0; j < len; j++)
			for(int k = j + 1; k < len; k++ )
				if(temp[i][j] > temp[i][k]) {
					t = temp[i][j];
					temp[i][j] = temp[i][k];
					temp[i][k] = t;				
				}
		//printf("%s\n",temp[i]);
	}

	/*	for(int i = 0; i < count ; i++)
		printf("%s\n", temp[i]);
		*/
	char te[100];
	for(int i = 0; i < count; i++)//把重复的单词覆盖掉
		for(int j = i + 1; j < count; j++)
			if(strcmp(temp[i],temp[j]) == 0) {
				mark[i] = 0;
				mark[j] = 0;
			}
	int num = 0;
	for(int i = 0; i < count; i++)
		if(mark[i]) {
			strcpy(last[num],word[i]);
			num++;
		}
		
	for(int i = 0; i < num; i++)
		for(int j = i + 1; j < num; j++)
			if(strcmp(last[i],last[j]) > 0) {
				strcpy(te,last[i]);
				strcpy(last[i],last[j]);
				strcpy(last[j],te);
			}	

	/*	for(int i = 0; i < count ; i++)
		printf("%s\n", temp[i]);
		*/		
	for(int i = 0; i < num; i++ )
			printf("%s\n", last[i]);
	return 0;
}



举报

相关推荐

0 条评论