#include <bits/stdc++.h>
using namespace std;
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 tree(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++){     
            tree(x+i*n,y+j*n,n);
            if(i==1&&j==1) cout << ")";
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> board[i];
    }
    tree(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: