poj1760 Disk Tree

来源:互联网 发布:牛鼻铣刀编程计算 编辑:程序博客网 时间:2024/04/30 11:16

先上题目:

3:Disk Tree
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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
WINNT\SYSTEM32\CONFIG
GAMES
WINNT\DRIVERS
HOME
WIN\SOFT
GAMES\DRIVERS
WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86
样例输出
GAMES
 DRIVERS
HOME
WIN
 SOFT
WINNT
 DRIVERS
 SYSTEM32
  CERTSRV
   CERTCO~1
    X86
  CONFIG
来源
Northeastern Europe 2000


数据结构较为直观:建立一个森林,然后对森林中的每一棵树进行先根遍历,打印结点信息(包含缩进和文字)。但是,题目要求根据字典顺序输出,这就要求树先根遍历输出的结点恰好也是按照字典顺序的。怎么做?一种办法是对输入的文本进行排序,这样在建立树的时候,结点的先根遍历顺序也是按照字典顺序的。


参考POJ1760,我了解到stl map的一条重要性质:在插入key-value对时,map总是可以保证key的有序性。下面我把目前对map的理解总结如下:

  1. map是一个容器,用于存放key-value对;key和value的数据类型可任意定义;对于容器中的某个元素p,(p->first)也就是key,(p->second)也就是value。
  2. map容器中元素的key值是唯一的,要么存在(只出现一次),要么不存在。可以用“map容器名.count(key)”判断,count函数的返回值只可能是0或1。
  3. map容器在插入元素(key-value对)时,保持key的有序性。
因此,数据结构可进一步描述如下:
  1. 申请一组容器map<string, int> m[MAXNODE];m[i]储存亲兄弟结点;特别地,m[0]储存森林中所有树的树根结点。
  2. 对于结点node=m[i][key],node->first为结点的名称(数据类型为string);node->second为此结点直系后代的“指针”(数据类型为int),即此结点所有的直系后代储存在m[node->second]中。
  3. 由map的性质,m[i]中所有元素的key总是保持有序。
于是题目中样例的存储结构如下图:


代码清单:
#include <string>#include <cstring>#include <cstdio>#include <map>using namespace std;#define MAXNODE 20000//输入文本每行最多40个结点,40*500#define MAXLEN 85#define MAXWORD 45map<string, int> m[MAXNODE];void preOder_traversal(int midx, int indentation){for (map<string, int>::iterator i=m[midx].begin(); i!=m[midx].end(); ++i){for (int i=0; i<indentation; ++i)printf(" ");printf("%s\n", (i->first).c_str());preOder_traversal(i->second, indentation+1);}}int main(){//freopen("D:\\in.txt", "r", stdin);//freopen("D:\\out.txt", "w", stdout);int n, i, j, midx, midx_counter=0;char str[MAXLEN];int word_init[MAXWORD];string current_str;scanf("%d", &n);while (n--){scanf("%s", str);//下面把str中的单词分割开来word_init[0]=0;j=1;for (i=0; str[i]; ++i)//0是空字符'\0'的十进制值{if(str[i]=='\\'){str[i]=0;word_init[j++]=i+1;//第j个单词从str[i+1]开始}}midx=0;//从树根开始for (i=0; i<j; ++i)//有j个单词{current_str=str+word_init[i];if(!m[midx].count(current_str))//新结点{m[midx][current_str]=(++midx_counter);}midx=m[midx][current_str];}}preOder_traversal(0, 0);return 0;}




0 0
原创粉丝点击