想象成括号匹配,dfs把同优先级的括号由小到大按字典序排个序就是树的最小表示法了
最大一个意思
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string a,b;
string treeMinPress(string s)
{
vector<string>v;
int st = 0;
int sum = 0;
for(int i = 0; i < s.size() ; i++)
{
if(s[i] == '0') sum++;
else sum--;
if(sum==0)
{
if(i-st+1 > 2) v.push_back("0" + treeMinPress(s.substr(st+1, i-st-1)) + "1");
else v.push_back("01") ;
st = i + 1;
}
}
sort(v.begin(), v.end());
string ans = "";
for(int i = 0; i < v.size(); i++) ans = ans + v[i];
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
cin >> a >> b;
a = treeMinPress(a);
b = treeMinPress(b);
if(a == b)printf("same\n");
else printf("different\n");
}
return 0;
}