fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, a[66][66];
  4.  
  5. string go(int y, int x, int size){
  6. bool isPass = true;
  7. string ret = "";
  8. for(int i = y; i < size; i++){
  9. for(int j = x; j < size; j++){
  10. if(a[y][x] != a[i][j]) isPass = false;
  11. }
  12. }
  13. if(isPass) ret += a[y][x];
  14. else {
  15. ret += "(";
  16. ret += go(y, x, size / 2);
  17. ret += go(y, x + size / 2, size / 2);
  18. ret += go(y + size / 2, x, size / 2);
  19. ret += go(y + size / 2, x + size / 2, size / 2);
  20. ret += ")";
  21. }
  22. return ret;
  23. }
  24.  
  25. int main(){
  26. scanf("%d", &n);
  27. for(int i = 0; i < n; i++){
  28. for(int j = 0; j < n; j++){
  29. scanf("%1d", &a[i][j]);
  30. }
  31. }
  32. string s = go(0, 0, n);
  33. printf("%d", s);
  34. }
Success #stdin #stdout 0.01s 5320KB
stdin
8
11110000
11110000
00011100
00011100
11110000
11110000
11110011
11110011
stdout
182410544