POJ - 3087 Shuffle’m Up
#include<iostream>
#include<set>
using namespace std;
string suff(string a, string b)
{
string ret;for (int i = 0; i < (int)a.size(); i++) ret += b[i], ret += a[i];return ret;
}
int main()
{
int T;cin >> T;for (int _ = 1; _ <= T; _++){
int n;cin >> n;string s1, s2, s3;cin >> s1 >> s2 >> s3;int ans = 1;set<string> mp;for (string t = suff(s1, s2); ; t = suff(t.substr(0, n), t.substr(n, n)), ans++){
if (mp.find(t) != mp.end()){
ans = -1; break;}else if (t == s3) break; else mp.insert(t);}cout << _ << ' ' << ans << endl;}return 0;
}