【题解】POJ - 3087 Shuffle'm Up 模拟

题目链接:http://poj.org/problem?id=3087

这是做kuangbin搜索专题遇到的,然后现在还是不会搜索,那就模拟吧
根据题目模拟,跳出循环的条件是s1数组出现了循环(证明永远不会得出s12)

ac代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,c,ans;
char s1[110],s2[110],s12[250];

int check(){
	char rec[110],tmp[250];
	strcpy(rec,s1);   //记录下s1数组留着后面判断
	ans=0;
	while(1){
		int i,j;
		for(i=0,j=0;i<c;i++){         //根据题目shuffle
			tmp[j++]=s2[i];
			tmp[j++]=s1[i];
		}
		tmp[j]=0;   //记得加这句,我一开始没加一直出不了答案
		ans++;
		if(!strcmp(s12,tmp)) return 1;    //两字符串相等
		for(int i=0;i<c;i++)
			s1[i]=tmp[i];
		for(int i=c,j=0;i<2*c;i++)
			s2[j++]=tmp[i];
		if(!strcmp(rec,s1)) return 0;   //s1数组出现了循环
	}
}

int main(){
	scanf("%d",&n);
	for(int t=1;t<=n;t++){
		scanf("%d %s %s %s",&c,s1,s2,s12);	
		if(check()) printf("%d %d\n",t,ans);
		else printf("%d -1\n",t);
	}
}
发布了46 篇原创文章 · 获赞 1 · 访问量 2428

猜你喜欢

转载自blog.csdn.net/weixin_43708069/article/details/99876689