大意就是按图中所示的方式洗牌,把两摞牌交替叠起来,然后再对半分成两摞(下边是S1上边是S2),之后重复进行洗牌,看到达所需情况需要洗牌多少次(不能达到输出-1)
Sample Input
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1
注意输出不止需要结果,还需要加上当前的轮数
核心步骤
代码
#include <iostream>
#include <string>
#include <set>
using namespace std;
set<string> states;
int main()
{
int times,length,step;
cin >> times;
string stack1,stack2,endCondition;
bool found;
for(int i=0;i<times;i++)
{
cin >> length;
cin >> stack1;
cin >> stack2;
cin >> endCondition;
step=0;
string temp = "";
found = false;
while(true)
{
for(int i=0;i<length;i++)
temp = temp+stack2[i]+stack1[i];
step++;
if(temp == endCondition)
{
cout << i+1<<" "<< step<<endl;
found = true;
}
if(states.find(temp)!=states.end()) //set数据的唯一性
break;
states.insert(temp);
stack1 = temp.substr(0,length); // 注意substr的使用
stack2 = temp.substr(length,length);
temp="";
}
if(!found)
cout << i+1 << " "<< -1<<endl;
}
return 0;
}