#include <bits/stdc++.h>
using namespace std;
int n;
string board[70];
bool check(int x,int y,int n) {
    for(int i=x; i<x+n; i++) {
        for(int j=y;j<y+n;j++) {
            if(board[x][y]!=board[i][j]) return false;
        }
    }
    return true;
}

void qtree(int x,int y,int n) {
   if(check(x,y,n)) {
       cout << board[x][y]; 
       return;
   }
    cout << '(';
    n/=2;
    for(int i=0; i<2; i++) {
        for(int j=0; j<2; j++) {
            qtree(x+i*n,y+j*n,n);
            if(i==1&&j==1) cout <<')';
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i=0; i<n;i++) {
        cin >> board[i];
        
    }
    qtree(0,0,n);
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: