题目通道
部分题目
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];
}