POJ 3087 Shuffle'm Up 解题报告

题目通道

部分题目

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

下述无效,将不对结果产生任何影响

The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

题解

简单的模拟,没有任何决策需要自己遍历。

提供AC代码

 #include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 100009
int main()
{
//	freopen("1.txt","r",stdin);
	int t; 
	t = read();
	map<string ,bool > vis; 
	rep(aai,1,t){
		vis.clear();
		int len; 
		int ans; 
		ans = 0; 
		string s1,s2,s12; 
		len = read();
		cin>>s1>>s2>>s12; 
		while ( 1 ){
			string str; 
			for (int j = 0,i=0; j < len; j++,i++){
				str += s2[i];
				str += s1[j];
			}
			if ( vis[str] ){
				ans = -1 ; 
				break; 
			}
			ans++;
			if ( str == s12){
				break; 
			}
			vis[str] = 1; 
			s1 = str.substr(0,str.size()/2);
			s2 = str.substr(str.size()/2,str.size());
		}
		printf("%d %d\n",aai,ans);
	}
	return 0;
}

值得注意

		s1 = str.substr(0,str.size()/2);
		s2 = str.substr(str.size()/2,str.size());
依旧是左闭右开。str这时是一个对象。
			vis.clear();
不处理掉将会影响下一次结果。
			for (int j = 0,i=0; j < len; j++,i++){
				str += s2[i];
				str += s1[j];
			}
string中的+重载帮了大忙。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值