hihoCoder 1383 The Book List (字典树变形)

大体题意:

给你一些文件的绝对路径,要求类似树的形式画出文件关系图?优先输出目录,在输出文件,都按照字典序小到大输出?

思路:

很明显,看到第一眼想到的是类似的字典树,这个也是一样的,根目录下有许多子节点,子节点可能是目论,也可能是文件。那就要再开个变量have表示是目录还是文件,然后建树分类讨论:

如果当前的名字是文件的话(最后一个),那么你只能放在和它一样名字的文件,或者重新开辟一个内存用于存放,

如果当前名字是目录 的话(不是最后一个),那么你只能放在和它一样名字的目录,或者重新开辟一个内存用于存放,

这样类字典树就构造好了!

然后是输出!

输出要求先输出目录,在文件, 两者都是字典序:

那么我们可以建立两个vector,一个存放目录,一个存放文件,然后两者排个序,依次递归输出即可!

注意:

如果要给结构体指针排序的话,必须自己写cmp,运算符重载死活不对= =!

最后在需要注意的是,字典树递归释放内存和清空变量即可!

坑:

其实这题是有坑的,就是有可能存在一个目录和一个文件名字是一样的,在建树时在开辟一个内存即可!

详细见代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
const int maxn = 107;
struct Node{
    string s;
    Node* next[maxn];
    int cur;
    bool have;
    Node (string s = ""):s(s),have(0){
        for (int i = 0; i < maxn; ++i)next[i] = NULL;
    }
}*root;
vector<string>v1;
bool cmp(const Node * lhs,const Node * rhs) {
    return lhs->s < rhs->s;
}
void Free(Node* cur){
    for (int i = 0; i < maxn; ++i){
        if (cur->next[i] != NULL)Free(cur->next[i]);
    }
    cur = NULL;
    delete cur;
}
void print(Node * node,int num){
    vector<Node*>f1,f2;
    for (int i = 0; i < node->cur; ++i){
        if (node->next[i]->have == 0)f1.push_back(node->next[i]);
        else {
            if (node->next[i]->cur == 0)f2.push_back(node->next[i]);
            else f1.push_back(node->next[i]);
        }
    }
    sort(f1.begin(),f1.end(),cmp);
    sort(f2.begin(),f2.end(),cmp);
    for (int i = 0; i < f1.size(); ++i){
        for (int j = 0; j < num*4; ++j)printf(" ");
        printf("%s\n",(f1[i]->s).c_str());
        print(f1[i],num+1);
    }
    for (int i = 0; i < f2.size(); ++i){
        for (int j = 0; j < num*4; ++j)printf(" ");
        printf("%s\n",(f2[i]->s).c_str());
        print(f2[i],num+1);
    }
}

int main() {
    string text;
    root = new Node();
    int kase = 0;
    while(getline(cin,text)){
        if (text == "0"){
            printf("Case %d:\n",++kase);
            print(root,0);
            Free(root);
            root = new Node();
            continue;
        }
        text += '/';
        int len = text.length();
        string tmp3 = "";
        v1.clear();
        for (int i = 0; i < len; ++i){
            if (text[i] == '/'){
                v1.push_back(tmp3);
                tmp3 = "";
                continue;
            }
            tmp3 += text[i];
        }
        Node * curr = root;
        len = v1.size();
        for (int j = 0; j < len; ++j){
            bool ok = 0;
            string tmp = v1[j];
            if (j == len-1){
                bool ok2 = 0;
                for (int i = 0; i < curr->cur; ++i){
                    if (curr->next[i]->s == tmp && curr->next[i]->have){
                        ok2 = 1;
                        curr = curr->next[i];
                        break;
                    }
                }
                if (!ok2){
                    curr->next[(curr->cur)++] = new Node(tmp);
                    curr = curr->next[(curr->cur)-1];
                }
                curr->have = 1;
                continue;
            }
            else {
                bool ok2 = 0;
                for (int i = 0; i < curr->cur; ++i){
                    if (curr->next[i]->s == tmp && curr->next[i]->have == 0){
                        ok2 = 1;
                        curr = curr->next[i];
                        break;
                    }

                }
                if (!ok2){
                    curr->next[(curr->cur)++] = new Node(tmp);
                    curr = curr->next[(curr->cur)-1];
                }
                continue;
            }
        }
    }
    return 0;
}

#1383 : The Book List

时间限制: 1000ms
单点时限: 1000ms
内存限制: 256MB

描述

The history of Peking University Library is as long as the history of Peking University. It was build in 1898. At the end of year 2015, it had about 11,000 thousand volumes of books, among which 8,000 thousand volumes were paper books and the others were digital ones. Chairman Mao Zedong worked in Peking University Library for a few months as an assistant during 1918 to 1919. He earned 8 Dayang per month there, while the salary of top professors in Peking University is about 280 Dayang per month.

Now Han Meimei just takes the position which Chairman Mao used to be in Peking University Library. Her first job is to rearrange a list of books. Every entry in the list is in the format shown below:

CATEGORY 1/CATEGORY 2/..../CATEGORY n/BOOKNAME

It means that the book BOOKNAME belongs to CATEGORY n, and CATEGORY n belongs to CATEGORY n-1, and CATEGORY n-1 belongs to CATEGORY n-2...... Each book belongs to some categories. Let's call CATEGORY1  "first class category", and CATEGORY 2 "second class category", ...ect. This is an example:

MATH/GRAPH THEORY
ART/HISTORY/JAPANESE HISTORY/JAPANESE ACIENT HISTORY
ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON LIUBEI
ART/HISTORY/CHINESE HISTORY/CHINESE MORDEN HISTORY
ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON CAOCAO

Han Meimei needs to make a new list on which the relationship between books and the categories is shown by indents. The rules are:

1) The n-th class category has an indent of  4×(n-1) spaces before it.
2) The book directly belongs to the n-th class category has an indent of  4×n spaces before it.
3) The categories and books which directly belong to a category X should be list below X in dictionary order. But all categories go before all books. 
4) All first class categories are also list by dictionary order.

For example, the book list above should be changed into the new list shown below:

ART
    HISTORY
        CHINESE HISTORY
            THREE KINDOM
                RESEARCHES ON CAOCAO
                RESEARCHES ON LIUBEI
            CHINESE MORDEN HISTORY
        JAPANESE HISTORY
            JAPANESE ACIENT HISTORY
MATH
    GRAPH THEORY

Please help Han Meimei to write a program to deal with her job.

输入

There are no more than 10 test cases.
Each case is a list of no more than 30 books, ending by a line of "0". 
The description of a book contains only uppercase letters, digits, '/' and spaces, and it's no more than 100 characters.
Please note that, a same book may be listed more than once in the original list, but in the new list, each book only can be listed once. If two books have the same name but belong to different categories, they are different books.

输出

For each test case, print "Case n:" first(n starts from 1), then print the new list as required.

样例输入
B/A
B/A
B/B
0
A1/B1/B32/B7
A1/B/B2/B4/C5
A1/B1/B2/B6/C5
A1/B1/B2/B5
A1/B1/B2/B1
A1/B3/B2
A3/B1
A0/A1
0
样例输出
Case 1:
B
    A
    B
Case 2:
A0
    A1
A1
    B
        B2
            B4
                C5
    B1
        B2
            B6
                C5
            B1
            B5
        B32
            B7
    B3
        B2
A3
    B1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值