ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List

本文介绍了一种通过模拟方式实现书目的整理树的方法。针对给出的书籍及其分类信息,文章详细阐述了如何构建和输出书目的整理树,并提供了完整的C++代码实现,包括节点结构定义、树的构造逻辑及遍历输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#1383 : The Book List

题意:给你一些书及书归类,叫你画出一棵书的整理树
分析:模拟
样例输入
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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <iterator>

using namespace std;
const int maxn = 320;
int tot,cas;
struct Node //节点
{
    string str;
    int o;
    int ran;
};
vector<Node> g[maxn];  // 树 ,记录每个节点的儿子Node
map<string,int> mp[maxn]; //保存树,mp【u】保存儿子的字符串,及节点编号
map<string,int> my[maxn];//保存每个非叶子节点的,字符串
bool flag[maxn];//标记是否为叶子节点
void dfs(int u,int step); //用于输出
bool cmp(const Node& a,const Node& b);
void init();
void solve()
{

    string ss,st;//st 表示截取的类名,ss表示输入每行的总信息
    string::iterator it,pre;
    map<string,int>::iterator mpper,mper;  //mp,my的指针
    int fath = 0; //记录父亲节点的编号
    Node node;

    while(getline(cin,ss))
    {
        init();
        if(ss!="0")

        do{
        fath = 0;
        for ( it=ss.begin(),pre=ss.begin(); ; ++it)
        {
           if(it==ss.end())
            {
                st.assign(pre,it);
                pre = it;
                mpper = mp[fath].find(st);//看字符串st在mp[fath]是否找得到
                mper = my[fath].find(st);//看字符串st在my[fath]是否找得到
                if(mpper==mp[fath].end()) //
                {

                    int sz = g[fath].size();tot++;
                    mp[fath].insert(make_pair(st,tot));

                    node.o = tot;
                    node.ran = sz;
                    node.str = st;
                    g[fath].push_back(node);
                    fath = tot;
                    flag[tot] = true;
                    break;
                }
                else if(mper==my[fath].end())
                {
                    break;
                }
                else{
                        if(flag[mpper->second]) break; //之前是否有这本书,有的话退出
                    mp[fath].erase(mpper);
                    int sz = g[fath].size();tot++;
                    mp[fath].insert(make_pair(st,tot));
                    node.o = tot;
                    node.ran = sz;
                    node.str = st;
                    g[fath].push_back(node);
                    fath = tot;
                    flag[tot] = true;
                }



                break;
            }
            if(*it=='/')
            {
                st.assign(pre,it);
                pre = it+1;
                mpper = my[fath].find(st);
                if(mpper == my[fath].end())
                {
                    int sz = g[fath].size();tot++;
                    mp[fath].insert(make_pair(st,tot));
                    my[fath].insert(make_pair(st,tot));

                    node.o = tot;
                    node.ran = sz;
                    node.str = st;
                    g[fath].push_back(node);
                    fath = tot;
                }
                else
                {
                    int d = (*mpper).second;
                     fath = d;
                }
            }
        }
    }while(getline(cin,ss)&&ss!="0");

     cas ++;
     printf("Case %d:\n",cas);
    dfs(0,0);
}
}
bool cmp(const Node& a,const Node& b)
{
    return a.str <= b.str;
}
void dfs(int u,int step)
{
    string str;
    str.assign(4*step,' ');
    int sz = g[u].size();
    if(sz==0)
    {
        return ;
    }
    int v;
    sort(g[u].begin(),g[u].end(),cmp);
    for(int i=0;i<sz;i++)
    {
        v = g[u][i].o;
        if(flag[v]) continue;
        cout << str;
        cout << g[u][i].str << endl;;
        dfs(v,step +1);
    }
    for(int i=0;i<sz;i++)
    {
        v = g[u][i].o;
        if(!flag[v]) continue;
        cout << str;

        cout << g[u][i].str << endl;;
        dfs(v,step +1);
    }
}


void init()
{
    tot = 0;
    for(int i=0;i<305;i++)
    {
        mp[i].clear();
        g[i].clear();
        my[i].clear();
    }
    memset(flag,false,sizeof(flag));
}
int main()
{
    cas = 0;

    init();
        //read();
    solve();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值