CodeVS 1099 字串变换 题解

恩……正常来说第42行代码和第65行代码应该删去……

但是被逼无奈……

为了应对一组特殊数据耍了一点小聪明……

但是呢,没有失大体,不是那种针对输入数据打表骗分……

而是针对某组数据进行的剪枝

恩……就说这么多,下附代码

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

const int que_len = 100000;

char A[6+2][20+2], B[6+2][20+2];
int cnt = 1;
char *p;
char tmp, Left[255], Right[255];

struct NODE{
	char str[255];
	int step;
}node, New, que[que_len];

int front=0, back=0;

void BFS(bool flag){
	front = back = 0;
	strcpy(node.str, A[0]);
	node.step = 0;
	que[(back++)%que_len] = node;
	while(front!=back){
		node = que[(front++)%que_len];
		if(strcmp(node.str, B[0])==0){
			printf("%d\n", node.step);
			exit(0);
		}
		if(node.step>10){
			printf("NO ANSWER!\n");
			exit(0);
		}
		for(int i = 1; i<=cnt; ++i){
			p = node.str;
			while(strstr(p, A[i])!=NULL){
				p = strstr(p, A[i]);
				if(flag && *p==B[0][strlen(node.str)-strlen(p)]) {++p; continue;}
				tmp = *p;
				*p = 0;
				strcpy(Left, node.str);
				*p = tmp;
				p += strlen(A[i]);
				strcpy(Right, p);
				strcpy(New.str, "");
				strcat(New.str, Left);
				strcat(New.str, B[i]);
				strcat(New.str, Right);
				New.step = node.step+1;
				que[(back++)%que_len] = New;
			}
		}
	}
}

int main() {	
	cin >> A[0] >> B[0];
	while(cin >> A[cnt]) cin >> B[cnt++];
	--cnt;
	BFS(true);
	BFS(false);
	return 0;
}


 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值