hihocoder 1384 the book list

2016北京网赛1001题

题目

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:

CN
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:

CN
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:

CN

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.

CN

Input - 输入
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.

CN

Output - 输出
For each test case, print “Case n:” first(n starts from 1), then print the new list as required.

题意

给你了一些文件的路径,这些路径,末尾是文件,其他的都是文件夹,一个文件夹下,同名的文件夹只有一个,同名的文件只有一个,但不同文件夹下无所谓。
让按一定顺序还原出整个文件系统。

思路

我想的是用字典树搞出整个系统,每个节点下存的分成文件夹和文件,文件夹可以向下延伸,文件就是存字符串,打印时分别排下序,然后先遍历文件夹,再打印字符串。但是出了不少问题,一是字典树就打过一次不太熟,二是没有好好读题不知道空格也算,之前用map+string存,只好一通大改。最后改得乱七八糟,好歹过了。后来听大佬解题,貌似先把’/’处理成码值比较小的,然后排序就可以了。总之,给巨巨们举个反例,一定好好好读题,读完好好想清啊!

代码:

写都写了,就贴一下吧。。。

#include <cstdio>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 7000;
struct node1
{
    char str[220];
    int index;
};
struct node2
{
    char str[220];
};
struct node {
    node1 fld[33];
    node2 file[33];
    int nfld;
    int nfile;
    char str[220];
} maps[maxn];
char str[405];
int sz =0; //结点总数
bool check1(int cur,char A[])
{
    for(int i=1;i<=maps[cur].nfile;i++)
    {
        if(strcmp(maps[cur].file[i].str,A)==0)
            return false;
    }
    return true;
}
int  check2(int cur,char A[])
{
    for(int i=1;i<=maps[cur].nfld;i++)
        if(strcmp(maps[cur].fld[i].str,A)==0)
            return maps[cur].fld[i].index;
    return -1;
}
void inserts(int index,int cur)//读了多少,当前节点位置
{
    char tmp[220];
    int num=0;
    memset(tmp,0,sizeof(tmp));
    int i,len=strlen(str);
    for(i=index;i<len;i++)
        if(str[i]=='/')
        {
            i++;
            break;
        }
        else
            tmp[num++]=str[i];
    tmp[num]='\0';
    if(i==len)
    {
        if(check1(cur,tmp))
            strcpy(maps[cur].file[++maps[cur].nfile].str,tmp);
    }
    else
    {
        int t=check2(cur,tmp);
        if(t!=-1)
        {
            inserts(i,t);
        }
        else
        {
            strcpy(maps[++sz].str,tmp);
            maps[sz].nfld=0;
            maps[sz].nfile=0;
            maps[cur].nfld++;
            maps[cur].fld[maps[cur].nfld].index=sz;
            strcpy(maps[cur].fld[maps[cur].nfld].str,tmp);
            inserts(i,sz);
        }
    }
    return ;
}
bool cmp1(node1 a,node1 b)
{
    return strcmp(a.str,b.str)<0;
}
bool cmp2(node2 a,node2 b)
{
    return strcmp(a.str,b.str)<0;
}
void query(int cur,int pos)//层数,maps里的位置
{
    if(pos!=0)
    {
        int t=cur-1;
        while(t--)
            printf("    ");
        printf("%s\n",maps[pos].str);
    }
    sort(maps[pos].fld+1,maps[pos].fld+1+maps[pos].nfld,cmp1);
    sort(maps[pos].file+1,maps[pos].file+1+maps[pos].nfile,cmp2);
    for(int i=1;i<=maps[pos].nfld;i++)
    {
        query(cur+1,maps[pos].fld[i].index);
    }
    for(int i=1;i<=maps[pos].nfile;i++)
    {
        int t=cur;
        while(t--)
            printf("    ");
        printf("%s\n",maps[pos].file[i].str);
    }
}
int main()
{
    int cases=0;
    while(gets(str)!=NULL)
    {
        if(strcmp(str,"0")==0)
        {
            printf("Case %d:\n",++cases);
            continue;
        }
        inserts(0,0);
        while( gets(str),strcmp(str,"0"))
            inserts(0,0);
        printf("Case %d:\n",++cases);
        query(0,0);
        sz=0;
        memset(maps,0,sizeof(maps));
        memset(str,0,sizeof(str));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值