Nine degrees OJ 1090: Path printing (tree, DFS)

Source: Internet
Author: User
Tags parent directory strcmp time limit

Time limit: 1 seconds

Memory Limit: 32 MB

Special sentence: No

Submitted: 1704

Solve: The description of the topic:

Give you a bunch of paths, for example:
A\b\c
A\d\e
B\cst
D\
You draw the directory structure that is contained in these paths, and the subdirectories are listed directly under the parent directory and are indented to the right by the parent directory, like this:
A
B
C
D
E
B
Cst
D
The same level needs to be arranged alphabetically and not in disorder. Input:

Each test case first behavior a positive integer n (n<=10) indicates that there are n paths, when n is 0 o'clock, the test ends, then there are n rows, each row has a string representing a path, and the length is less than 50. Output:

Output directory structure, the output of each test sample is immediately followed by a blank line. Sample Input:

4
a\b\c
a\d\e
b\cst
d\
0
Sample output:
A
  b
    c
  d
    e
b
  CST
D

Source:The real problem of computer research in Shanghai Jiaotong University 2005

Ideas:

Create the appropriate tree based on the input data, and then the DFS output.


Code:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define N #define LEN typedef s
    Truct Node {char Name[len];
    int num;
struct node *children[n];
     
Node;
    void Create (Node *root, char S[len]) {if (s[0] = = ' n ') return;
    int I, J;
    Char Name[len];
    i = 0;
        while (s[i]!= ' \ \ && s[i]!= ' i ') {name[i] = s[i];
    i + +;
    } Name[i] = ';
    if (s[i] = = ' \ ') i++;
    printf ("s=%s, name=%s\n", S, name);
    For (j=0 j<root->num; j + +) {if (strcmp (root->children[j]->name, name) = = 0) break;
    } if (J < root->num)//find root = root->children[j];
        else {node *N1 = (node *) malloc (sizeof (node));
        strcpy (n1->name, name);
        N1->num = 0;
        root->children[root->num++] = N1;
    root = N1;
Create (root, s+i); int cmp (const void *A, const void *b) {Node **x = (Node * *) A;
    Node **y = (Node * *) b;
Return strcmp ((*x)->name, (*y)->name);
    } void DFS (Node *root, int addlen) {int i;
        if (Addlen >= 0) {for (i=0; i<addlen; i++) printf ("");
    printf ("%s\n", root->name);
    } qsort (Root->children, Root->num, sizeof (Node *), CMP);
    For (i=0 i<root->num; i++) {DFS (root->children[i), Addlen + strlen (root->name) + 1);
    int main (void) {int n;
    int i;
    Char S[len];
 
    Node Root;
        while (scanf ("%d", &n)!= EOF && N) {root.name[0] = ' yes ';
        Root.num = 0;
            For (i=0 i<n; i++) {scanf ("%s", s);
        Create (&root, s);
        DFS (&root,-1);
    printf ("\ n"); }/************************************************************** problem:1090 user:liangrx06 Language: C result:accepted time:0 Ms Memory:912 KB ****************************************************************/ 


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.