fork(3) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n;
  7. while( cin >> n && n){
  8. int count = 0;
  9. char *first = new char[n+1];
  10. char *second = new char[n+1];
  11. char *third = new char[2 * n+1];
  12. char *result = new char[2 * n+1];
  13. cin >> first;
  14. cin >> second;
  15. cin >> third;
  16. while(strcmp(result, third) && (count < 51)){
  17. for(int i = 0; i < n; i++){
  18. result[2 * i] = second[i];
  19. result[2 * i + 1] = first[i];
  20. }
  21. strncpy(first, &result[0], n);
  22. strncpy(second, &result[n], n);
  23. count++;
  24. }
  25. cout << ((count < 51) ? count : -1) << endl;
  26. }
  27. return 0;
  28. }
Success #stdin #stdout 0s 15240KB
stdin
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
9
ACCABCABC
DEFDEFDEF
ECECECAFAFAFDBDBDB
10
ABCDEFGHCD
AAAAAAAAAA
BDFHDAAAAAACEGCAAAAA
100
ABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCD
ABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCD
CEGCACEGCACEGCACEGCACEGCADFHDBDFHDBDFHDBDFHDBDFHDBEGCACEGCACEGCACEGCACEGCACFHDBDFHDBDFHDBDFHDBDFHDBDGCACEGCACEGCACEGCACEGCACEHDBDFHDBDFHDBDFHDBDFHDBDFCACEGCACEGCACEGCACEGCACEGDBDFHDBDFHDBDFHDBDFHDBDFH
5
AAAAA
BBBBB
AABBBAAABB
6
AAAAAA
BBBBBB
AAABBBAAABBB
7
AAAAAAA
BBBBBBB
ABBAABBAABBAAB
8
AAAAAAAA
BBBBBBBB
BBAABBAABBAABBAA
9
AAAAAAAAA
BBBBBBBBB
BBBBAAAAABBBBBAAAA
10
AAAAAAAAAA
BBBBBBBBBB
BBAABBAABBAABBAABBAA
0
stdout
2
-1
-1
5
30
9
11
2
2
8
2