题目连接:uva 1556 - Disk Tree

题目大意:给出N个文件夹关系,然后依照字典序输出整个文件文件夹。

解题思路:以每一个文件夹名作为字符建立一个字典树就可以,每一个节点的关系能够用map优化。

#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn = 50005;
typedef map<string, int>::iterator iter; struct Tire {
int sz;
map<string, int> g[maxn]; void init();
void insert(string s);
void put(int u, int d);
}tree; int main () {
int n;
string s;
while (cin >> n && n) {
tree.init();
for (int i = 0; i < n; i++) {
cin >> s;
s += '\\';
tree.insert(s);
}
tree.put(0, 0);
cout << endl;
}
return 0;
} void Tire::init() {
sz = 1;
g[0].clear();
} void Tire::insert(string s) { int u = 0;
string word = ""; for (int i = 0; i < s.length(); i++) {
if (s[i] == '\\') { if (!g[u].count(word)) {
g[sz].clear();
g[u][word] = sz++;
} u = g[u][word];
word = "";
} else
word += s[i];
}
} void Tire::put (int u, int d) { for (iter i = g[u].begin(); i != g[u].end(); i++) {
for (int j = 0; j < d; j++)
cout << " ";
cout << i->first << endl;
put(i->second, d + 1);
}
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

uva 1556 - Disk Tree(特里)的更多相关文章

  1. ural1067 Disk Tree

    Disk Tree Time limit: 2.0 secondMemory limit: 64 MB Hacker Bill has accidentally lost all the inform ...

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

    Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8132   Accepted: 1949 Desc ...

  3. UVA 11922 Splay tree

    UVA 11922 题意: 有n个数1~n 操作a,b表示取出第a~b个数,翻转后添加到数列的尾部 输入n,m 输入m条指令a,b 输出最终的序列 代码: #include<iostream&g ...

  4. HDU 1504 Disk Tree

    转载请注明出处:http://blog.csdn.net/a1dark 分析:查了一下这题.发现网上没有什么关于这道题的解题报告.其实题目意思挺好懂的.就是给你一些文件的目录结构.然后让你把它们组合在 ...

  5. 1067. Disk Tree(字符串)

    1067 破题啊  写完发现理解错题意了 子目录下会有跟之前重名的 把输入的字符串存下来 排下序 然后依次找跟上面有没有重的 #include <iostream> #include< ...

  6. 【HDOJ】1504 Disk Tree

    文件可以重名.先按字典序将路径排序,再过滤掉公共前缀.其中的问题是'\'的ASCII比[A-Z0-9]大,将它替换为空格.否则字典序有问题. /* 1504 */ #include <iostr ...

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

    题目 题目     分析 莫名A了     代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void buil ...

  8. uva 6910 - Cutting Tree 并查集的删边操作,逆序

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. 【习题 6-11 UVA - 10410】Tree Reconstruction

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 可以先确定当前这棵子树的dfs序的范围. 然后第一个元素肯定是这棵子树的根节点. 那么只要在这棵子树的范围里面枚举节点. 看看有没有 ...

随机推荐

  1. 求pi 的公式

    pi = 3.1415926..... 下面用c 语言来求解PI 现有公式 (pi*pi)/6 = 1 + 1/(2*2) + 1/(3*3) + ... + 1/(n*n); #include &l ...

  2. 动态创建ImageView

    1.布局文件 <LinearLayout android:id="@+id/viewGroup" android:layout_width="wrap_conten ...

  3. UVa 11879 - Multiple of 17

    称号:计算一个数字是不17倍数. 说明:串,睑板. 简单的问题,直接推论可以是. 设定 n = 10a + d:(0 ≤ d ≤ 9) a - 5d = 51a - 5n,假设n被17整除,这个数必定 ...

  4. RH033读书笔记(3)-Lab 4 Browsing the Filesystem

    Lab 4 Browsing the Filesystem Sequence 1: Directory and File Organization 1. Log in as user student ...

  5. TCP和UDP的差别

    简单的差别: TCP提供面向连接的.可靠的数据流传输,而UDP提供的是非面向连接的.不可靠的数据流传输. TCP传输单位称为TCP报文段,UDP传输单位称为用户数据报. TCP注重数据安全性,UDP传 ...

  6. HDU 3836 Equivalent SetsTarjan+缩点)

    Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...

  7. 原生javascript与jquery 的比较

    JavaScript的优点和缺点: 优点: 性能:由于JavaScript运行在客户端,节省了web服务器的请求时间和带宽 轻量级的脚本语言,比较容易学习 运行在用户机器上,运行结果和处理相对比较快. ...

  8. java.lang.ClassFormatError

    Error occurred during initialization of VMjava.lang.ClassFormatError: Unknown constant tag 26 in cla ...

  9. c++日历改进版

    #include<iostream> # include<fstream> #include<time.h> #include<string> #inc ...

  10. Cookie概念

    ASP.NET Cookie概念.CURD操作.原理.实际运用   会话就WEB开发来说,一个会话就是你通过浏览器与服务器之间的一次通话,只不过这种通话是以用浏览器浏览的方式来实现的. 就会话的应用来 ...