POJ 3087 Shuffle'm Up
题目大意:
给你两堆都为(n)块的堆,交叉得到一个新的堆。然后将新堆再分成两堆,再合并,再分(...),一直到出现符合要求的堆,输出次数,若不可能输出(-1)。
题解:
模拟洗牌的过程,用map记录当前情况是否重复出现。
#include <iostream>
#include <string>
#include <map>
using namespace std;
int t, len;
string ans, s1, s2;
int solve() {
map <string, int> vis;
string temp = s1 + s2;
int step = 0;
while (temp != ans && !vis[temp]) {
vis[temp] = 1;
step++;
string nxt = "";
for (int i = 0; i < len; ++i) {
nxt += temp[i + len];
nxt += temp[i];
}
temp = nxt;
}
if (temp == ans) return step;
return -1;
}
int main() {
cin >> t;
for (int i = 1; i <= t; ++i) {
cin >> len;
cin >> s1 >> s2 >> ans;
cout << i << " " << solve() << endl;
}
return 0;
}