给定字符串建树

给定字符串建树_第1张图片

//
#include !

using namespace std;

#define fi first
#define se second

#define pb push_back
#define es erase
#define in insert

#define pii pair
#define ll long long
#define lb long double

#define ioss                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define mp(i, j) make_pair(i, j)
#define mem(a, x) memset(a, x, sizeof(a))
#define all(x) x.begin(), x.end()
#define fcout(i)
#define endl "\n"

const ll maxn = 2e5 + 7;
vector<char> mp[maxn];
string s;
void dfs(string s)
{
    cout << s << endl;
    int c = 1, rt = s[0];
    string sub;
    for (int i = 2; i < s.size(); i++) {
        if (s[i] == ',')
            continue;
        if (s[i] == '(')
            c++;
        else if (s[i] == ')') {
            c--;
            if (c == 1) {
                sub += ')';
                dfs(sub);
                sub.clear();
                continue;
            }
        } else if (c == 1)
            mp[rt].pb(s[i]);
        sub += s[i];
    }
    cout << char(rt) << " :: ";
    for (int i = 0; i < mp[rt].size(); i++)
        cout << mp[rt][i] << " ";
    cout << endl;
}
int main()
{
    cin >> s;
    s = '#' + s;
    dfs(s);
}

你可能感兴趣的:(学习,dfs,图论,树结构)