题意
传送门 POJ 3087
题解
可以猜测 s 12 s_{12} s12 状态可以在有限时间内遍历完,那么模拟即可,若出现 s 12 s_{12} s12 等于目标字符串的状态,则输出第一次转移到该状态的 s h u f f l e shuffle shuffle 次数;若进入循环且未出现目标字符串,则可以判断无法转移至目标状态。
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#include <string>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 105
char s1[maxn], s2[maxn], s12[maxn << 1], s[maxn << 1];
int main()
{
int n, c;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d %s %s %s", &c, s1, s2, s);
set<string> st;
int res = 0;
s12[c << 1] = '\0';
while (1)
{
for (int i = 0, j = 0; i < c; i++)
{
s12[j++] = s2[i], s12[j++] = s1[i];
}
++res;
if (strcmp(s, s12) == 0)
{
break;
}
if (st.find(s12) != st.end())
{
res = -1;
break;
}
st.insert(s12);
for (int i = 0; i < c; i++)
{
s1[i] = s12[i], s2[i] = s12[i + c];
}
}
printf("%d %d\n", i, res);
}
return 0;
}