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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值