题目连接: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);
}
}
分享到:
相关推荐
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
uva705 Slash Maze 的代码,在UVaOJ上通过
PDF试题
uva532 Dungeon Master的源代码,并且AC了
Algorithm-UVA-Solutions-in-Python.zip,python 3中各种uva(acm)问题的解决方案。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
这是UVA133 TheDoleQueue救济金发放问题,经典的算法问题。初学算法的人要对这种算法非常熟悉并且能熟练运用。
c++的算法题四分树的实现-uva10771
tpcw-nyu-uva-client 客户端