【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>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 typedef struct path_t {
 43     char s[84];
 44     int len;
 45     
 46     friend bool operator< (const path_t& a, const path_t& b) {
 47         return strcmp(a.s, b.s) < 0;
 48     }
 49 } path_t;
 50 
 51 const int maxn = 505;
 52 char SP[1005];
 53 path_t path[maxn];
 54 
 55 int main() {
 56     ios::sync_with_stdio(false);
 57     #ifndef ONLINE_JUDGE
 58         freopen("data.in", "r", stdin);
 59         freopen("data.out", "w", stdout);
 60     #endif
 61     
 62     int t, n;
 63     int mlen, len;
 64     int i, j, k;
 65     char name[15];
 66     int beg, nspace;
 67     char *s, *ss;
 68     
 69     memset(SP, ' ', sizeof(SP));
 70     
 71     scanf("%d", &t);
 72     while (t--) {
 73         scanf("%d", &n);
 74         for (i=0; i<n; ++i) {
 75             scanf("%s", path[i].s);
 76             path[i].len = strlen(path[i].s);
 77             for (j=0; j<path[i].len; ++j)
 78                 if (path[i].s[j] == '\\')
 79                     path[i].s[j] = ' ';
 80             path[i].s[path[i].len] = ' ';
 81         }
 82         sort(path, path+n);
 83         rep(ii, 0, n) {
 84             s = path[ii].s;
 85             len = path[ii].len;
 86             if (ii) {
 87                 ss = path[ii-1].s;
 88                 mlen = min(len, path[ii-1].len);
 89                 nspace = 0;
 90                 beg = 0;
 91                 for (i=0; i<=mlen; ++i) {
 92                     if (s[i] != ss[i])
 93                         break;
 94                     if (ss[i] == ' ') {
 95                         nspace++;
 96                         beg = i + 1;
 97                     }
 98                 }
 99             } else {
100                 beg = 0;
101                 nspace = 0;
102             }
103             
104             i = beg;
105             j = 0;
106             while (i <= len) {
107                 if (s[i] == ' ') {
108                     SP[nspace] = '\0';
109                     name[j] = '\0';
110                     printf("%s%s\n", SP, name);
111                     SP[nspace++] = ' ';
112                     j = 0;
113                 } else {
114                     name[j++] = s[i];
115                 }
116                 ++i;
117             }
118         }
119         if (t)
120             putchar('\n');
121     }
122     
123     #ifndef ONLINE_JUDGE
124         printf("time = %d.\n", (int)clock());
125     #endif
126     
127     return 0;
128 }

 

posted on 2015-11-14 17:32  Bombe  阅读(172)  评论(0编辑  收藏  举报

导航