AnagramsbyStack_1004

1.没有注意i, o后面都是跟随一个空格的。错了两次。先是完全没有空格,然后是最后一个有效数据后以为不用空格

2.没有注意栈在深度搜索的时候数据也要恢复到前一次,才能退出这次操作


#include <stdio.h>
#include <string.h>
#include <memory.h>

#define INPUT_STRING_MAX 30
char g_Input[INPUT_STRING_MAX];
char g_Output[INPUT_STRING_MAX];
char g_Stack[INPUT_STRING_MAX];
char g_OperatorOutput[INPUT_STRING_MAX*4];
char g_OperatorIndex = 0;
char g_iStackIndex = 0;
char g_iStringLen = 0;
char g_iInputIndex = 0;
char g_iOutIndex = 0;

void DFSOperator()
{
	// 尝试input
	if(g_iInputIndex < g_iStringLen)
	{
		// input
		char tempStackData = g_Stack[g_iStackIndex];
		g_Stack[g_iStackIndex++] = g_Input[g_iInputIndex++];
		g_OperatorOutput[g_OperatorIndex*2] = 'i';
		g_OperatorIndex++;

		DFSOperator();

		// 恢复
		g_OperatorIndex--;
		g_iStackIndex--;
		g_iInputIndex--;
		g_Stack[g_iStackIndex] = tempStackData;
	}

	// 尝试output
	if(g_iStackIndex > 0 && g_Stack[g_iStackIndex-1] == g_Output[g_iOutIndex])
	{
		g_iStackIndex--;
		g_iOutIndex++;
		g_OperatorOutput[g_OperatorIndex*2] = 'o';
		g_OperatorIndex++;
		if (g_iOutIndex == g_iStringLen)
		{
			// 一种方式出现,输出结果
			printf("%s\n", g_OperatorOutput);
		}
		else
		{
			DFSOperator();
		}

		// 恢复
		g_OperatorIndex--;
		g_iStackIndex++;
		g_iOutIndex--;
	}
}

int main()
{
	while(scanf("%s", g_Input) != EOF && scanf("%s", g_Output) != EOF)
	{
		// 清理现场
		g_iStringLen = strlen(g_Input);

		memset(g_OperatorOutput, ' ', sizeof(g_OperatorOutput));
		g_OperatorOutput[g_iStringLen*4] = 0;

		g_OperatorIndex = 0;
		g_iStackIndex = 0;
		g_iInputIndex = 0;
		g_iOutIndex = 0;

		printf("[\n");

		DFSOperator();

		printf("]\n");
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值