hoj 1081 Scramble Sort

Background

In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word.


Input

The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single
period.


Output

For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period.


Sample Input

0.
banana, strawberry, OrAnGe.
Banana, StRaWbErRy, orange.
10, 8, 6, 4, 2, 0.
x, 30, -20, z, 1000, 1, Y.
50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle.
.


Sample Output

0.
banana, OrAnGe, strawberry.
Banana, orange, StRaWbErRy.
0, 2, 4, 6, 8, 10.
x, -20, 1, Y, 30, 1000, z.
-100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm.

大致题意是:给一个字符串,里面有数字,单词,对数字和单词进行排序。要求排序后不能打乱原先顺序(即原先是数字的地方还是数字,原先是单词的地方还是单词)。

说说我的思路吧:首先把单词和数字分开(数字可能是负数),创建两张链表,一张保存数字,一张保存单词并记录其在原先字符串中的位置(把数字也当作字符串)然后分别对两张表排序,数字以升序,单词以字典序。

特别的对单词排序比较时因为题意不区分大小写,故不能直接使用strcmp函数。于是我自己写了一个字符串比较函数

仍然是返回-1,1,0三种情况,如下:

int MyStrcmp(const char *_str1, const char *_str2)
{
	while (*_str1!= '\0' && *_str2!= '\0')
	{
		char c1 = *_str1;
		char c2 = *_str2;
		if (c1 >= 'A' && c1 <= 'Z')
		{
			c1 += 'a' - 'A';
		}
		if (c2 >= 'A' && c2 <= 'Z')
		{
			c2 += 'a' - 'A';
		}
		if (c1 != c2)
			return c1 > c2 ? 1 : -1;
		_str1++, _str2++;
	}
	if (*_str1 == '\0' && *_str2 == '\0')
		return 0;
	return *_str1 > *_str2 ? 1 : -1;
}

剩下的工作就简单了,输出时将两张表合并,以索引index顺序保存到结果字符数组中。最后输出即可。

PS:开始我用fgets函数接收输入的字符串,结果反复的提交,反复的超时。最后改成gets函数,ok,AC 敲打

具体代码如下:

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_Size 102
int index = 0;
char buf[Max_Size] = { 0 };
typedef struct node
{
	int index;
	char data[20];
	struct node *pNext;
}Node,*pNode;
pNode pS[Max_Size], pN[Max_Size];
int pSt = 0, pNt = 0;
pNode Add_New_Node(int index,char *data)
{
	pNode NewNode = (pNode)malloc(sizeof(Node));
	NewNode->index = index;
	strcpy(NewNode->data, data);
	NewNode->pNext = NULL;
	return NewNode;
}
void ListInertNum(pNode *pHead, int index, char *data)
{
	if (*pHead == NULL)
	{
		*pHead = Add_New_Node(index, data);
		pN[pNt++] = *pHead;
	}
	else
	{
		pNode p = *pHead;
		while (p->pNext != NULL)
		{
			p = p->pNext;
		}
		p->pNext = pN[pNt++] = Add_New_Node(index, data);
	}
}
void ListInertStr(pNode *pHead, int index, char *data)
{
	if (*pHead == NULL)
	{
		*pHead = Add_New_Node(index, data);
		pS[pSt++] = *pHead;
	}
	else
	{
		pNode p = *pHead;
		while (p->pNext != NULL)
		{
			p = p->pNext;
		}
		p->pNext = pS[pSt++] = Add_New_Node(index, data);
	}
}
int MyStrcmp(const char *_str1, const char *_str2)
{
	while (*_str1!= '\0' && *_str2!= '\0')
	{
		char c1 = *_str1;
		char c2 = *_str2;
		if (c1 >= 'A' && c1 <= 'Z')
		{
			c1 += 'a' - 'A';
		}
		if (c2 >= 'A' && c2 <= 'Z')
		{
			c2 += 'a' - 'A';
		}
		if (c1 != c2)
			return c1 > c2 ? 1 : -1;
		_str1++, _str2++;
	}
	if (*_str1 == '\0' && *_str2 == '\0')
		return 0;
	return *_str1 > *_str2 ? 1 : -1;
}
void SortListByStr(pNode pHead)
{
	int i, j;
	for (i = 0; i < pSt - 1; i++)
	{
		for (j = i + 1; j < pSt; j++)
		{
			if (-1 == MyStrcmp(pS[j]->data, pS[i]->data))
			{
				pNode temp = pS[i];
				pS[i] = pS[j];
				pS[j] = temp;

				int t = pS[i]->index;
				pS[i]->index = pS[j]->index;
				pS[j]->index = t;
			}
		}
	}
}
void SortListByNum(pNode pHead)
{
	int i, j;
	for (i = 0; i < pNt - 1; i++)
	{
		for (j = i + 1; j < pNt; j++)
		{
			if (atoi(pN[i]->data) > atoi(pN[j]->data))
			{
				pNode temp = pN[i];
				pN[i] = pN[j];
				pN[j] = temp;

				int t = pN[i]->index;
				pN[i]->index = pN[j]->index;
				pN[j]->index = t;
			}
		}
	}
}
char *GetInfo(char *dest,char *str)
{
	int i, n;
	if (strchr(str, ',')!= NULL)
		n = strchr(str, ',') - str;
	else
		n = strchr(str, '.') - str;
	memset(dest, 0, (strlen(dest)+1) * sizeof(char));
	for (i = 0; i < n; i++)
		dest[i] = *str++;
	dest[i] = '\0';
	return str;
}
void solve(char *source,pNode *pStr,pNode *pNum)
{
	char *p = source, temp[30] = { 0 };
	while (*p != '.')
	{
		if ((*p >= '0' && *p <= '9') || *p == '-')
		{
			p = GetInfo(temp, p);
			ListInertNum(&(*pNum), index++, temp);
		}
		else if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'z'))
		{
			p = GetInfo(temp, p);
			ListInertStr(&(*pStr), index++, temp);
		}
		else if (*p == ',' || *p == ' ')
		{
			p++;
		}
	}
}
void PrintResult(pNode pStr,pNode pNum)
{
	int i, j, k;
	SortListByStr(pStr), SortListByNum(pNum);
	pNode p = (pStr == NULL ? pNum : pStr);
	while (p->pNext != NULL)
		p = p->pNext;
	p->pNext = (pStr == NULL ? pStr : pNum);
	k = 0;
	for (i = 0; i < index; i++)
	{
		p = (pStr == NULL ? pNum : pStr);
		while (p != NULL)
		{
			if (p->index == i)
			{
				for (j = 0; j < strlen(p->data); j++)
				{
					buf[k++] = p->data[j];
				}
				if (i < index - 1)
				{
					buf[k++] = ',';
					buf[k++] = ' ';
				}
				else
					buf[k++] = '.';
			}
			p = p->pNext;
		}
	}
	buf[k] = '\0';
	printf("%s\n", buf);
}
void FreeList(pNode *pStr, pNode *pNum)
{
	pNode p = (*pStr == NULL ? *pNum :* pStr), p1 = NULL;
	p1 = p->pNext;
	while (p1 != NULL)
	{
		p = p1;
		p1 = p1->pNext;
		free(p);
	}
	free(*pStr == NULL ? *pNum : *pStr);
}
int main()
{
	while (1)
	{
		index = 0 ,pSt = 0 ,pNt = 0;
		gets(buf);
		if (0 == strcmp(buf, "."))
			break;
		pNode pStr = NULL, pNum = NULL;
		solve(buf, &pStr, &pNum);
		PrintResult(pStr, pNum);
		FreeList(&pStr,&pNum);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值