北京赛区(2016)网络赛A-The Book List-数据结构+DFS

题目大意:给你若干个字符串,每段字符串代表一本书,字符串中间会有'/'把各个字符串隔开成一段一段的,最后一个就是书,前面的都为目录,要求输出的时候按目录层层递进,如果两个目录都在同一个父目录之下,他们就按字典序排序,如果那个是书的话,就把书放在最后;

题目解析:首先这肯定是个数据结构的题目,肯定是树,对于每棵树我们要保存它的子孙树,且需要保存它本身的字符串值,那么我们再输入每一本书的时候就需要构造树,构造的时候需要枚举看父目录是否已经生成,从而层层递进,这里就用到了DFS,枚举的时候要注意是否那个节点是书(只需要判断它的子孙是不是为空就可以了)。

AC代码:


#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].child.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;
}

一个崭新的开始来啦!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值