157. 树形地铁系统

https://www.acwing.com/problem/content/159/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

string dfs(string &seq, int &u)
{
    u ++ ;  // downward, '0'
    vector<string> seqs;
    while (seq[u] == '0') seqs.push_back(dfs(seq, u));
    u ++ ;  // upward, '1'

    sort(seqs.begin(), seqs.end());
    string res = "0";
    for (auto &s : seqs) res += s;
    res += '1';
    return res;
}

string solve(string a, string b) {
    a = '0' + a + '1';
    b = '0' + b + '1';
    int ua = 0, ub = 0;
    auto ra = dfs(a, ua), rb = dfs(b, ub);

    if (ra == rb) {
        return "same";
    } else {
        return "different";
    }
}

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        string a, b;
        cin >> a >> b;
        cout << solve(a, b) << endl;
    }
    return 0;
}

 

posted @ 2019-05-06 11:12  7ydiat  阅读(155)  评论(0编辑  收藏  举报