G - Shuffle'm Up

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <map>
 5 using namespace std;
 6 
 7 map<string,int>q;
 8 
 9 int main(){
10     int t;
11     cin >> t;
12     int cnt = 0;
13     while(t--){
14         cnt++;
15         int n;
16         cin >> n;
17         string s1,s2,s3;
18         cin >> s1 >> s2 >> s3;
19         string ss;
20         int ans = 0;
21         while(1){
22             ans++;
23             ss ="";
24             for(int i = 0;i < n;i++){
25                 ss = ss + s2[i] + s1[i];
26             }
27             if(ss == s3){
28                 break;
29             }
30             else if(q[ss] == 1){
31                 ans = -1;
32                 break;
33             }
34             else{
35                 q[ss] = 1;
36                 s1 = ss.substr(0,n);
37                 s2 = ss.substr(n,n+n);
38             }
39         }
40         cout << cnt << " " << ans << endl;
41     }
42     return 0;
43 }

 

posted @ 2018-05-05 21:13  ouyang_wsgwz  阅读(129)  评论(0编辑  收藏  举报