Description of the topic link Timus 1067
This topic troubled for a long time, the back of the idea, the solution is also very clear, the result is timed out!
There are some things you can do to write code:
After the idea of a good to write code will be more smooth, while thinking about the idea of writing code efficiency is lower!
First put the code here, time-out code is not good to say what, in the future can learn something! First time with new and delete, compare counseling! (2.046s>2.0s TLE)
#include <iostream> #include <algorithm>using namespace std;struct disk{int num;//The Numbering of the folder is useful when sorting int sonnum; Number of child nodes int son[120]; child node corresponding to the number char s[10]; Own folder name};D ISK D[20002],root[505];int index[505],id; Top-level corresponding number, top-level number int n,dnum; Number of file paths folders char s[100]; File path int search (char *q,int num) {int i;if (num==-1) {for (i=0;i<id;i++) {if (strcmp (d[index[i]].s,q) ==0) return index [i]; Returns the top-level folder number}return Dnum; }else//In the corresponding numbered folder, find out if there is a folder number with the same name as your own folder {for (i=0;i<d[num].sonnum;i++) {if (strcmp (Q,D[D[NUM].SON[I]].S) ==0) return D[num].son[i]; Returns the number of subfolders}return Dnum;}} void input () {int i,j;for (i=0;i<n;i++) {char tmp[100];scanf ("%s", s); bool F=false; Falseint Kt=0,rnum is determined each time the first folder; Represents the directory or subdirectory under the Rnum folder for (J=0;j<strlen (s); J + +) {if (s[j]== ' \ \ ' &&f==false) {tmp[kt]= '} '; Rnum=search (tmp , -1); if (rnum==dnum)//top-level folder does not have a match, add a new top-level folder {d[dnum].num=dnum;strcpy (d[dnum].s,tmp);d [dnum].sonnum=0;// The number of child nodes is initialized 0index[id++]=dnum;dnum++;} Kt=0;f=true;} else if (s[j]== ' \ \ ') {tmp[kt]= ' + '; int rTmp=search (Tmp,rnum); if (rtmp==dnum)//The folder name is not found in the existing subfolder, add a new folder {d[rnum].son[d[rnum].sonnum]=dnum;strcpy (d[ DNUM].S,TMP);d [Dnum].num=dnum; d[dnum].sonnum=0;d[rnum].sonnum++;d num++;} rnum=rtmp;kt=0;} else tmp[kt++]=s[j];} tmp[kt]= ' + '; if (f==false) {rnum=search (tmp,-1); if (rnum==dnum)//top-level folder does not have a match, add a new top-level folder {d[dnum].num=dnum;strcpy ( D[DNUM].S,TMP);d [dnum].sonnum=0; The number of child nodes is initialized 0index[id++]=dnum;dnum++;}} Else{int Rtmp=search (Tmp,rnum); if (rtmp==dnum)//existing subfolders do not find the name of the folder, add a new folder {D[rnum].son[d[rnum].sonnum]=dnum; strcpy (d[dnum].s,tmp);d [Dnum].num=dnum; d[dnum].sonnum=0;d[rnum].sonnum++;d num++;}}} BOOL CMP (Disk aa,disk BB)//define collation sorted by dictionary order from small to large {return strcmp (AA.S,BB.S) <0;} void Dfs (int nnum,int level)//search from first-tier subdirectory, sort output {int i,j;if (d[nnum].sonnum) {Disk *w=new disk[505];for (I=0;i<d[nnum]. sonnum;i++) {w[i]=d[d[nnum].son[i]]; printf ("1:%s\n", W[I].S);} Sort (w,w+d[nnum].sonnum,cmp), for (i=0;i<d[nnum].sonnum;i++) {for (j=0;j<level;j++) printf ("");p rintf ("%s\n", W[I].S);//printf ("%s\n", d[d[nnum].son[i]].s); int X=w[i].num;//dfs (d[nnum].son[i],level+1);d FS (x,level+1);} Delete [] w;}} int main () {while (scanf ("%d", &n)!=-1) {id=0;dnum=0;input (); int i,j; for (i=0;i<id;i++) root[i]=d[index[i]]; Sort (root,root+id,cmp);//sort (d,d+dnum,cmp); for (i=0;i<id;i++) {int num=root[i].num; printf ("%s\n", ROOT[I].S); DFS (num,1);}} return 0;}
Timus 1067 Disk Tree "Timeout Code"