Disk Tree

Title Description

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 "WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86") 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.

Entry

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 ("!#$%&'()-@^_`{}~").

Export

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.

Sample input

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

Sample Output

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

Messing around code that can only be handed over in schools OJ, POJ handed timeout

Want to start a file name is not repeated, positive solution is the manual chain tree, binary multi-turn fork, add nodes when ordering, vector messing around simulate a bit slow, too lazy to change the

#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("*****测试*****\n");
        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("*****测试*****\n");*/
        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("\n");
    }
     
    return 0;
}

Guess you like

Origin www.cnblogs.com/sz-wcc/p/11071961.html