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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章