zoukankan      html  css  js  c++  java
  • Disk Tree

    题目描述

    Hacker Bill has accidentally lost all the information from his workstation's hard drive and he has no backup copies of its contents. He does not regret for the loss of the files themselves, but for the very nice and convenient directory structure that he had created and cherished during years of work. Fortunately, Bill has several copies of directory listings from his hard drive. Using those listings he was able to recover full paths (like "WINNTSYSTEM32CERTSRVCERTCO~1X86") for some directories. He put all of them in a file by writing each path he has found on a separate line. Your task is to write a program that will help Bill to restore his state of the art directory structure by providing nicely formatted directory tree.

    输入

    The first line of the input file contains single integer number N (1 <= N <= 500) that denotes a total number of distinct directory paths. Then N lines with directory paths follow. Each directory path occupies a single line and does not contain any spaces, including leading or trailing ones. No path exceeds 80 characters. Each path is listed once and consists of a number of directory names separated by a back slash ("").

    Each directory name consists of 1 to 8 uppercase letters, numbers, or the special characters from the following list: exclamation mark, number sign, dollar sign, percent sign, ampersand, apostrophe, opening and closing parenthesis, hyphen sign, commercial at, circumflex accent, underscore, grave accent, opening and closing curly bracket, and tilde ("!#$%&'()-@^_`{}~").

    输出

    Write to the output file the formatted directory tree. Each directory name shall be listed on its own line preceded by a number of spaces that indicate its depth in the directory hierarchy. The subdirectories shall be listed in lexicographic order immediately after their parent directories preceded by one more space than their parent directory. Top level directories shall have no spaces printed before their names and shall be listed in lexicographic order. See sample below for clarification of the output format.

    样例输入

    7
    WINNTSYSTEM32CONFIG
    GAMES
    WINNTDRIVERS
    HOME
    WINSOFT
    GAMESDRIVERS
    WINNTSYSTEM32CERTSRVCERTCO~1X86

    样例输出

    GAMES
     DRIVERS
    HOME
    WIN
     SOFT
    WINNT
     DRIVERS
     SYSTEM32
      CERTSRV
       CERTCO~1
        X86
      CONFIG
    

    瞎搞代码,只能在学校OJ上交过,POJ上交超时

    一开始想成文件名不重复了,正解是手动链树,多叉转二叉,添加节点的时候排序,vector瞎搞模拟有点慢,懒得改了

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    using namespace std;
    int n,cnt,T;
    struct E{
        string s;
        int pla;
        vector<int>v;
    };
    E fir[520];
    E node[20020];
    int fircnt=0;
    int cmp(E x,E y){
        return x.s<y.s;
    }
    int cmp1(int x,int y){
        return node[x].s<node[y].s;
    }
    void dfs(int cur,int h){
        if(cur==0) return;
        if(h==0){
            cout<<fir[cur].s<<endl;
            sort(fir[cur].v.begin(),fir[cur].v.end(),cmp1);
            for(int i=0;i<fir[cur].v.size();i++){
                dfs(fir[cur].v[i],h+1);
            }
        }
        else{
            for(int i=1;i<=h;i++) printf(" ");
            cout<<node[cur].s<<endl;
            sort(node[cur].v.begin(),node[cur].v.end(),cmp1);
            for(int i=0;i<node[cur].v.size();i++){
                dfs(node[cur].v[i],h+1);
            }
        }
    }
     
    int main(){
        //freopen("I.in","r",stdin);
        //freopen("I.out","w",stdout);
        //cin>>T;
        T=1;
        while(T--){
            //for(int i=1;i<=cnt;i++) a[i].clear();
            cnt=fircnt=0;
            //memset(e,0,sizeof(e));
            //memset(root,0,sizeof(root));
            //mp.clear();
            cin>>n;
            string s0;
            for(int i=1;i<=n;i++){
                cin>>s0;
                int x1=-1,x2=-1;
                string tmps="";//,last="";
                int last=0,tmp,cur=0;
                for(int j=0;j<s0.size();j++){
                    if(x2==-1&&s0[j]=='\'){
                        cur++;
                        x2=j;
                        tmps=s0.substr(0,j);
                        tmp=-1;
                        for(int k=1;k<=fircnt;k++){
                            if(tmps==fir[k].s){
                                tmp=k;
                                break;
                            }
                        }
                        if(tmp==-1){
                            ++fircnt;
                            fir[fircnt]=(E){tmps,fircnt};
                            last=fircnt;
                        }
                        else{
                            last=fir[tmp].pla;
                        }
                        //cout<<tmps<<' ';
                    }
                    else if(s0[j]=='\'){
                        cur++;
                        x1=x2,x2=j;
                        tmps=s0.substr(x1+1,x2-x1-1);
                        tmp=-1;
                        //if(tmps=="SYSTEM32")cout<<"!!!";
                        if(cur==2){
                             
                            for(int k=0;k<fir[last].v.size();k++){
                                if(tmps==node[fir[last].v[k]].s){
                                    tmp=k;
                                    break;
                                }
                            }
                            if(tmp==-1){
                                ++cnt;
                                node[cnt]=(E){tmps,cnt};
                                fir[last].v.push_back(cnt);
                                last=cnt;
                            }
                            else{
                                last=node[fir[last].v[tmp]].pla;
                            }
                            /*if(tmps=="SYSTEM32"){
                                cout<<"^^"<<tmp;
                                cout<<"&&"<<last<<"&&"<<endl;
                            }*/
                        }
                        else{
                            /*if(tmps=="CERTSRV"){
                                cout<<"&&"<<last<<"&&"<<endl;
                            }*/
                            for(int k=0;k<node[last].v.size();k++){
                                if(tmps==node[node[last].v[k]].s){
                                    tmp=k;
                                    break;
                                }
                            }
                             
                            if(tmp==-1){
                                ++cnt;
                                node[cnt]=(E){tmps,cnt};
                                node[last].v.push_back(cnt);
                                last=cnt;
                            }
                            else{
                                last=node[node[last].v[tmp]].pla;
                            }
                        }
                        //cout<<tmps<<' ';
                    }
                }
                if(x1==-1&&x2==-1){
                    cur++;
                    tmps=s0.substr(0,s0.size());
                    tmp=-1;
                    for(int k=1;k<=fircnt;k++){
                        if(tmps==fir[k].s){
                            tmp=k;
                            break;
                        }
                    }
                    if(tmp==-1){
                        ++fircnt;
                        fir[fircnt]=(E){tmps,fircnt};
                        last=fircnt;
                    }
                    else{
                        last=fir[tmp].pla;
                    }
                    //cout<<tmps<<' ';
                }
                else if(s0[s0.size()-1]!='\'){
                    cur++;
                    tmps=s0.substr(x2+1,s0.size()-x2-1);
                    tmp=-1;
                    if(cur==2){
                        for(int k=0;k<fir[last].v.size();k++){
                            if(tmps==node[fir[last].v[k]].s){
                                tmp=k;
                                break;
                            }
                        }
                        if(tmp==-1){
                            ++cnt;
                            node[cnt]=(E){tmps,cnt};
                            fir[last].v.push_back(cnt);
                            last=cnt;
                        }
                        else{
                            last=node[fir[last].v[tmp]].pla;
                        }
                    }
                    else{
                        for(int k=0;k<node[last].v.size();k++){
                            if(tmps==node[node[last].v[k]].s){
                                tmp=k;
                                break;
                            }
                        }
                        if(tmp==-1){
                            ++cnt;
                            node[cnt]=(E){tmps,cnt};
                            node[last].v.push_back(cnt);
                            last=cnt;
                        }
                        else{
                            last=node[node[last].v[tmp]].pla;
                        }
                    }
                    //cout<<tmps<<' ';
                }
                //cout<<endl;
            }
             
            sort(fir+1,fir+1+fircnt,cmp);
            /*printf("*****测试*****
    ");
            for(int i=1;i<=cnt;i++){
                cout<<e[i].s<<":";
                for(int j=0;j<a[i][0];j++){
                    cout<<e[a[i][j]].s<<' ';
                }
                cout<<endl;
            }
            dfstest(trieroot);
            printf("*****测试*****
    ");*/
            for(int i=1;i<=fircnt;i++){
                dfs(i,0);
            }
            /*cout<<"**********"<<endl;
            for(int i=1;i<=fircnt;i++){
                cout<<fir[i].s<<endl;
            }
            cout<<"************"<<endl;
            for(int i=1;i<=cnt;i++){
                cout<<node[i].s<<endl;
            }*/
            if(T) printf("
    ");
        }
         
        return 0;
    }
  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/sz-wcc/p/11071961.html
Copyright © 2011-2022 走看看