157. 树形地铁系统 - AcWing题库
中心车站是树根,0往下走,1往上走,看看两个字符串代表的树的结构是否一样
#include <bits/stdc++.h>
#include <iostream>
#include<unordered_map>
#include<unordered_set>
#define x first
#define y second
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
typedef pair<int,int> PII ;
typedef pair<LL,int> PLI ;
const int N = 2e5+ 10 ;
const LL INF2 = 1e17;
int n,m;
string dfs(string s,int &u)
{
u ++ ;//跳过最前面加的0
vector<string> q;
while(s[u] == '0') q.push_back(dfs(s,u));// 能往下走
u ++ ;
sort(q.begin(),q.end());
string res = "0";
for(auto &t:q) res += t;
res += '1';
return res;
}
inline void solve()
{
string a,b;cin >> a >> b;
a= '0' + a + '1';b= '0' + b + '1';
int ua = 0,ub=0;
string ra =dfs(a,ua),rb = dfs(b,ub);
if(ra == rb) puts("same");
else puts("different");
}
signed main()
{
ios
int T=1;
cin>>T;
while(T -- )
{
solve();
}
return 0;
}