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;
}


CodeVS 1099 字串变换 题解

 

 

 

上一篇:成功解决Address localhost:1099 is already in use


下一篇:【ZZULIOJ】1099: 角谷猜想(多实例测试)