树形地铁系统(树的最小表示)

该程序解决的是一个关于树形地铁线路的问题,通过DFS深度优先搜索算法对输入的两个字符串表示的地铁线路进行重排,然后比较重排后的线路是否一致。如果一致,输出same,否则输出different。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值