码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 1556 - Disk Tree

时间:2015-05-03 10:40:05      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题目:给你一下文件的路径,对文件的路径按照字典序的输出,如果有子目录,下载父目录的下面,

           前面加上和深度相同的空格。

分析:字典树,字符串。将路径分解成多级的文件夹和文件,然后按字典序多级排序即可。

            这里利用字典树作为存储的数据结构,便于查询。

说明:注意去重。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const int dictsize = 128;
const int nodesize = 20002;
const int wordsize = 20002;

//Tire
typedef struct _tnode
{
	int 	flag;
	_tnode 	*next[dictsize];
}tnode;
tnode dict[nodesize];

char list[wordsize][10];

class Tire
{
	private:
		int 	size;
		int 	count;
		tnode* 	root;
	public:
		Tire() {initial();}
		void initial() {
			memset(dict, 0, sizeof(dict));
			size = count = 0; root = newnode();
		}
		tnode* newnode() {return &dict[size ++];}    
        int insert(char* word) {    
            tnode* now = root;    
            for (int i = 0 ; word[i] ; ++ i) {
                if (!now->next[word[i]])    
                    now->next[word[i]] = newnode();    
                now = now->next[word[i]];    
            }  
            if (!now->flag) {
				now->flag = ++ count;
				strcpy(list[count], word);  
			}
            return now->flag; 
        } 
};
//

typedef struct _dnode
{
	int id[40];
}dnode;
dnode line[wordsize];

char buf[88],temp[10];

bool cmp(dnode a, dnode b)
{
	for (int i = 0;; ++ i) {
		if (a.id[i] == -1)
			return 1;
		if (b.id[i] == -1)
			return 0;
		int value = strcmp(list[a.id[i]], list[b.id[i]]);
		if (value)
			return value < 0; 
	}
	return 0;
}

int main()
{
	int t,n,start,count,sum,first = 1;
	scanf("%d",&t);
	while (t --) {
		scanf("%d",&n);
		Tire tire;
		memset(line, 0, sizeof(line));
		sum = 0;
		for (int i = 0; i < n; ++ i) {
			scanf("%s",buf);
			start = count = 0;
			for (int j = 0; buf[j]; ++ j) {
				if (buf[j] == '\\') {
					line[sum].id[count] = tire.insert(temp);
					line[sum].id[++ count] = -1;
					start = j+1;
					for (int k = 0; k < count; ++ k)
						line[sum+1].id[k] = line[sum].id[k];
					++ sum;
				}else {
					temp[j-start] = buf[j];
					temp[j-start+1] = 0;
				}
			}
			line[sum].id[count] = tire.insert(temp);
			line[sum].id[++ count] = -1;
			++ sum;
		}
				
		sort(line, line+sum, cmp);
		count = -1,start = -1;
		for (int i = 0; i < sum; ++ i) {
			for (int j = 0; line[i].id[j] != -1; ++ j) {
				if (line[i].id[j+1] == -1) {
					if (j == count && start == line[i].id[j])
						break;
					for (int k = 0; k < j; ++ k)
						printf(" ");
					printf("%s\n",list[line[i].id[j]]);
					start = line[i].id[j];
					count = j;
					break;
				}
			}
		}
		if (t) printf("\n");
	}
    return 0;
}


UVa 1556 - Disk Tree

标签:

原文地址:http://blog.csdn.net/mobius_strip/article/details/45457013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!