UVa 1556 - Disk Tree
来源:互联网 发布:淘宝小号用什么注册 编辑:程序博客网 时间:2024/05/17 22:22
题目:给你一下文件的路径,对文件的路径按照字典序的输出,如果有子目录,下载父目录的下面,
前面加上和深度相同的空格。
分析:字典树,字符串。将路径分解成多级的文件夹和文件,然后按字典序多级排序即可。
这里利用字典树作为存储的数据结构,便于查询。
说明:注意去重。
#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;//Tiretypedef 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;}
0 0
- UVa 1556 - Disk Tree
- UVA 1556 - Disk Tree(Trie)
- uva 1556 - Disk Tree(字典树)
- Disk Tree
- Disk Tree
- HDU 1504 Disk Tree
- poj1760 Disk Tree
- poj-1760 Disk Tree
- POJ 1760 Disk Tree 笔记
- UVA Tree
- Tree UVA
- timus 1067 Disk Tree【超时代码】
- disk
- UVa 548 Tree
- uva 548 Tree
- uva 112 Tree Summing
- uva 112 - Tree Summing
- uva 548 - Tree
- javascript学习实录 之二(数组操作等等utils)
- android looper
- Dijskra(有误)
- 移动Web前端开发_横向UL以及自适应屏幕_div定位的方式居中
- Android核心基础-5.Android 数据存储与访问-3.使用Sqlite进行数据存储
- UVa 1556 - Disk Tree
- javascript学习实录 之一 -
- 极限方程式程序和27级存档_android
- OC 数字对象和字符串对象
- json-lib 的maven dependency
- Android模仿微信语音聊天功能
- 2015年阿里实习生面试总结
- 数据结构 - 哈希函数
- HDU 1285 确定比赛名次