NOIP 2002 字串变换 解题报告

  挺好的题目,以后记住,当出现初始状态和目标状态求最短路径的时候就用双向DFS,速度快很多!!!
  双向BFS的资料:http://www.cnblogs.com/yylogo/archive/2011/07/25/double-bfs.html

  代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct {
	char str[401];
	int sep;
}q1[20000], q2[20000];
int h1, r1, h2, r2;
char s1[6][21], s2[6][21];
int l1[6], l2[6];
int n;

void newcopy2(int start, int use)
{
	int i, j;
	r2++;
	q2[r2].sep = q2[h2].sep + 1;
	for(i = 0; i < start; i++){
		q2[r2].str[i] = q2[h2].str[i];
	}
	for(j = 0; s1[use][j] != '\0'; j++, i++){
		q2[r2].str[i] = s1[use][j];
	}
	for(j = start + strlen(s2[use]); q2[h2].str[j] != '\0'; j++, i++){
		q2[r2].str[i] = q2[h2].str[j];
	}
	for(i = 0; i < r1; i++){
		if(strcmp(q1[i].str, q2[r2].str) == 0){
			printf("%d\n", q1[i].sep + q2[r2].sep);
			exit(0);
		}
	}
}

void newcopy1(int start, int use)
{
	int i, j;
	r1++;
	q1[r1].sep = q1[h1].sep + 1;
	for(i = 0; i < start; i++){
		q1[r1].str[i] = q1[h1].str[i];
	}
	for(j = 0; s2[use][j] != '\0'; j++, i++){
		q1[r1].str[i] = s2[use][j];
	}
	for(j = start + strlen(s1[use]); q1[h1].str[j] != '\0'; j++, i++){
		q1[r1].str[i] = q1[h1].str[j];
	}
	for(i = 0; i < r1; i++){
		if(strcmp(q2[i].str, q1[r1].str) == 0){
			printf("%d\n", q2[i].sep + q1[r1].sep);
			exit(0);
		}
	}
}

void deal(void)
{
	int i, j;
	while(h1 <= r1 && h2 <= r2){
		if(q1[h1].sep + q2[h2].sep > 10){
			printf("NO ANSWER!\n");
			exit(0);
		}
		for(i = 0; i < strlen(q1[h1].str); i++){
			for(j = 0; j < n; j++){
				if(strncmp(s1[j], &q1[h1].str[i], strlen(s1[j])) == 0){
					newcopy1(i, j);
				}
			}
		}
		h1++;
		for(i = 0; i < strlen(q2[h2].str); i++){
			for(j = 0; j < n; j++){
				if(strncmp(s2[j], &q2[h2].str[i], strlen(s2[j])) == 0){
					newcopy2(i, j);
				}
			}
		}
		h2++;
	}
}

int main(int argc, char **argv)
{
	scanf("%s%s", q1[0].str, q2[0].str);
	while(scanf("%s%s", s1[n], s2[n]) == 2){
		n++;
	}
	deal();
	printf("NO ANSWER!\n");
	return 0;
}

  

posted @ 2011-07-27 20:13  zqynux  阅读(1793)  评论(3编辑  收藏  举报