BEIJING (2016) online race a-the book list-data structure +dfs

Source: Internet
Author: User
Tags cas parent directory

The main idea: to give you a number of strings, each string represents a book, the middle of the string will have '/' to separate the string into a paragraph, the last is the book, the front is the directory, the output is required by the directory layer of progressive, if two directories are under the same parent directory, they are sorted by dictionary order, If that is a book, put the book at the end;

Topic Analysis: First of all this is certainly a data structure of the topic, it must be a tree, for each tree we want to save its descendants tree, and need to save its own string value, then we enter each book when we need to construct the tree, the construction of the time need to enumerate to see if the parent directory has been generated, so that the level of progressive DFS is used here, the enumeration should be noted whether the node is a book (only need to judge its descendants is not empty on it).

AC Code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <
String> #include <vector> using namespace std;
    struct Tree {vector<tree>child;
String value;
};
    void Dfs (string s,tree& t) {string temp,temp1;
    int I,j,k,flag;
        if (s!= "") {temp= "";
                For (I=0;i<s.length (); i++) {if (s[i]!= '/') {temp+=s[i];
            K=i;
                } else {k=i;
            Break
        }} flag=0;
        Temp1= "";
        For (J=k+1;j<s.length (); j + +) Temp1+=s[j]; For (I=0;i<t.child.size (); i++) {if (t.child[i].value==temp&&s[k]== '/' &&t.child[i].ch
                Ild.size () >0) {flag=1;
            Break } if (t.child[i].value==temp&& (K+1==s.length ()) &&t.child[i].chilD.size () ==0) {flag=1;
            Break
        }}//cout<<temp<<endl;
        cout<<temp1<<endl;
            if (flag==0) {tree x;
            X.value=temp;
            T.child.push_back (x);
            Cout<<t.child.size () <<endl;
        if (temp1!= ") Dfs (Temp1,t.child[t.child.size ()-1]);
        } else {if (temp1!= "") Dfs (Temp1,t.child[i]);
    } S=TEMP1;
        }} BOOL CMP (tree A,tree b) {if (A.child.size () ==0&&b.child.size () >0) return false;
        else if (A.child.size () >0&&b.child.size () ==0) return true;

else return a.value<b.value;
    } void Solve (tree& t) {int i;
    Sort (T.child.begin (), T.child.end (), CMP);
For (I=0;i<t.child.size (); i++) solve (t.child[i]);
    } void Display (tree t,int N) {int i,j;
        For (I=0;i<t.child.size (), i++) {for (j=0;j<4* (n-1); j + +) printf ("");
        cout<<t.child[i].value<<endl;
   Display (t.child[i],n+1);
    }} int main () {int cas;
    Char str[110];
    string S;
    Tree root;
    Root.value= "";
    Cas=1;
        while (Cin.getline (str,110)) {s=str;
        if (s!= "0") Dfs (S,root);
            else {solve (root);
            printf ("Case%d:\n", CAs);
            Display (root,1);
            cas++;
        Root.child.clear ();
    }}//cout<<root.child.size () <<endl;
return 0;
 }

It's a brand new start.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.