UVa 1556 Disk Tree

方法: Trie

其实就是建树,node里存文件名。因为最后同目录下的文件要按照文件名顺序输出,所以储存child pointer的时候用了map,自带排序。下面的code是动态分配内存,然而用数组也可以,参见trie template。

Code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71
 72
 73 struct node
 74 {
 75     map<string, node*> child;
 76 };
 77 stack<node*> inuse;
 78 stack<node*> ready;
 79 void clear()
 80 {
 81     while (!inuse.empty())
 82     {
 83         ready.push(inuse.top());
 84         ready.top()->child.clear();
 85         inuse.pop();
 86     }
 87 }
 88 node* new_node()
 89 {
 90     if (ready.empty())
 91     {
 92         node* temp = new node();
 93         ready.push(temp);
 94     }
 95     auto ret = ready.top();
 96     ready.pop();
 97     return ret;
 98 }
 99 int n;
100 node *root;
101 void add(const string &str)
102 {
103     int last = 0;
104     int len = str.length();
105     node *cur = root;
106     for (int i = 0; i <= len; ++i)
107     {
108         if (i == len || str[i] == ‘\\‘)
109         {
110             string nxt = str.substr(last, i-last);
111             last = i+1;
112             if (cur->child.count(nxt) == 0)
113             {
114                 cur->child[nxt] = new_node();
115             }
116             cur = cur->child[nxt];
117         }
118     }
119 }
120 void print(node* cur, int space)
121 {
122     const string s = string(space, ‘ ‘);
123     for (auto &pr : cur->child)
124     {
125         cout << s << pr.first << newline;
126         print(pr.second, space+1);
127     }
128 }
129 int main()
130 {
131     while (cin >> n)
132     {
133         root = new_node();
134         string line;
135         rep(i, n)
136         {
137             cin >> line;
138             add(line);
139         }
140         print(root, 0);
141         clear();
142         cout << newline;
143     }
144
145 }
146 /*
147  2
148  3 2 cat dog mouse rat bat 1 1 abc cab
149  */

时间: 2024-10-08 02:27:24

UVa 1556 Disk Tree的相关文章

uva 1556 - Disk Tree(字典树)

题目连接:uva 1556 - Disk Tree 题目大意:给出N个目录关系,然后按照字典序输出整个文件目录. 解题思路:以每个目录名作为字符建立一个字典树即可,每个节点的关系可以用map优化. #include <cstdio> #include <cstring> #include <map> #include <string> #include <iostream> #include <algorithm> using nam

UVA 1556 - Disk Tree(Trie)

UVA 1556 - Disk Tree 题目链接 题意:给定一些字符串,表示目录,要求输出整体目录的结构 思路:跟Trie树差不多,只不过是每个结点存放的是一个字符串,利用map映射即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string> #include <map> #include &l

Disk Tree

Disk Tree Time limit: 2.0 secondMemory limit: 64 MB 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

POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)

Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8132   Accepted: 1949 Description LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, w

timus 1067 Disk Tree【超时代码】

题目的描述链接timus 1067 这个题目困扰了很久,后面理清思路了,解法也很明确,结果是超时! 在写代码时也收获了一些: 在把思路理好后去写代码会更顺利点,边想思路边写代码效率比较低! 先把代码放在这里,超时的代码不好说什么,以后还可以学到一些东西!第一次用new 和delete,比较怂!(2.046s>2.0s TLE) #include<iostream> #include<algorithm> using namespace std; struct Disk { i

【HDOJ】1504 Disk Tree

文件可以重名.先按字典序将路径排序,再过滤掉公共前缀.其中的问题是'\'的ASCII比[A-Z0-9]大,将它替换为空格.否则字典序有问题. 1 /* 1504 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector

【UVA】536 Tree Recovery(树型结构基础)

题目 题目 ? ? 分析 莫名A了 ? ? 代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void build(int l1,int r1,int l2,int r2) { int root=l1,p=l2; if(l1>r1) return; while(s2[p]!=s1[root] && p<=r2) p++; int cnt=p-l2; build(l1+1,l1+cnt,l2,

Linux平台下源码安装mysql多实例数据库

Linux平台下源码安装mysql多实例数据库[[email protected] ~]# netstat -tlunp | grep 330tcp6 0 0 :::3306 :::* LISTEN 6191/mysqld [[email protected] ~]# ss -tlunp | grep 330tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=6191,fd=10)) [[email protected] ~]# syst

[2016-02-08][UVA][548][Tree]

UVA - 548 Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root