码迷,mamicode.com
首页 > 其他好文 > 详细

礼盒问题

时间:2020-02-04 00:50:45      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:sub   spl   stream   content   pac   har   empty   while   start   

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>

using namespace std;

struct Node
{
    int is_box;
    string item;
    vector <struct Node *>childs;
};

void splie_contents(string str, vector <string>& contents)
{
    int pos = 0;
    int start;
    int end;
    stack <int> st;

    while(pos < str.size()) {
        pos = str.find(',');
        if(pos == string::npos) {
            contents.push_back(str);
            break;
        } else {
            contents.push_back(str.substr(0, pos));
        }

        pos++;
        if(str[pos] == '[') {
            start = pos;
            end = start+1;
            st.push(start);

            while(!st.empty() && end < str.size()) {
                if(str[end] == '[') {
                    st.push(end);
                } else if(str[end] == ']') {
                    st.pop();
                }

                end++;
            }

            contents.push_back(str.substr(start, end-start));
            pos = end+1;
        }

        if(pos < str.size()) {
            str = str.substr(pos);
        }
    }
}

struct Node *build_tree(string str)
{
    struct Node *root;
    struct Node *child;
    vector <string> contents;
    int pos = 0;

    cout << "build_tree: " << str << endl;

    root = new struct Node;

    /*items*/
    if(str[0] != '[') {
        root->is_box = 0;
        root->item = str;
        return root;
    }

    /*empty box*/
    if(str[0] == '[' && str[1] == ']') {
        root->is_box = 1;
        return root;
    }

    root->is_box = 1;

    /*split all items*/
    splie_contents(str.substr(1, str.size() - 2), contents);
    for(int i=0; i<contents.size(); i++) {
        cout << contents[i] << " ";
    }
    cout << endl;

    for(vector <string>::iterator it = contents.begin(); it < contents.end(); it++) {
        cout << *it << " ";
        child = build_tree(*it);
        root->childs.push_back(child);
    }

    cout << endl;

    return root;
}

void print_tree(struct Node *root)
{
    queue <struct Node *> que;
    struct Node *node;
    struct Node *flag;

    que.push(root);
    flag = root;

    while(!que.empty()) {
        node = que.front();

        if(!node->is_box) {
            cout << node->item << " ";
        } else {
            cout << "[] ";
        }

        if(node == flag) {
            if(!node->childs.empty()) {
                flag = node->childs[node->childs.size()-1];
            }

            cout << endl;
        }

        for(vector <struct Node*>::iterator it = node->childs.begin(); it<node->childs.end(); it++) {
            que.push(*it);
        }

        que.pop();
    }
}

int main(int argc, char **argv)
{
    //string str("[T-shirt,[doll,[dog,cat],[]]]");
    string str("[doll,[dog,cat],[]]");
    struct Node *root;

    root = build_tree(str);
    print_tree(root);

    return 0;
}

礼盒问题

标签:sub   spl   stream   content   pac   har   empty   while   start   

原文地址:https://www.cnblogs.com/joechow/p/12258004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!