【HDOJ】1504 Disk Tree

时间:2022-11-01 15:54:14

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

 /* 1504 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct path_t {
char s[];
int len; friend bool operator< (const path_t& a, const path_t& b) {
return strcmp(a.s, b.s) < ;
}
} path_t; const int maxn = ;
char SP[];
path_t path[maxn]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t, n;
int mlen, len;
int i, j, k;
char name[];
int beg, nspace;
char *s, *ss; memset(SP, ' ', sizeof(SP)); scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i=; i<n; ++i) {
scanf("%s", path[i].s);
path[i].len = strlen(path[i].s);
for (j=; j<path[i].len; ++j)
if (path[i].s[j] == '\\')
path[i].s[j] = ' ';
path[i].s[path[i].len] = ' ';
}
sort(path, path+n);
rep(ii, , n) {
s = path[ii].s;
len = path[ii].len;
if (ii) {
ss = path[ii-].s;
mlen = min(len, path[ii-].len);
nspace = ;
beg = ;
for (i=; i<=mlen; ++i) {
if (s[i] != ss[i])
break;
if (ss[i] == ' ') {
nspace++;
beg = i + ;
}
}
} else {
beg = ;
nspace = ;
} i = beg;
j = ;
while (i <= len) {
if (s[i] == ' ') {
SP[nspace] = '\0';
name[j] = '\0';
printf("%s%s\n", SP, name);
SP[nspace++] = ' ';
j = ;
} else {
name[j++] = s[i];
}
++i;
}
}
if (t)
putchar('\n');
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}