zoukankan      html  css  js  c++  java
  • CERC2017 H Hidden Hierarchy(树+模拟)

    题意:

    在一些给定的目录里按要求展开到制定大小并按字典序输出

    思路:

    因为有目录这个东西,所以想到模拟一个类似字典树的东西,不过这里每个儿子可能有n个节点,而且不能O(1)查询了

    代码超长。。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<stack>
    #include<queue>
    #include<deque>
    #include<set>
    #include<vector>
    #include<map>
    #include<functional>
        
    #define fst first
    #define sc second
    #define pb push_back
    #define mem(a,b) memset(a,b,sizeof(a))
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define lc root<<1
    #define rc root<<1|1
    #define lowbit(x) ((x)&(-x)) 
    
    using namespace std;
    
    typedef double db;
    typedef long double ldb;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> PI;
    typedef pair<ll,ll> PLL;
    
    const db eps = 1e-6;
    const int mod = 1e9+7;
    const int maxn = 2e6+100;
    const int maxm = 2e6+100;
    const int inf = 0x3f3f3f3f;
    const db pi = acos(-1.0);
    
    
    
    //vector<string>str;//每个编号对应的字符串
    string str[90000 + 10];
    bool cmp(int a, int b){
        return str[a] < str[b];
    }
    vector<int>v[90000 + 10];//v[i]为i的孩子们
    int tol = 1;
    int vis[90000 + 10];//结尾节点
    int vl[90000 + 10];//节点的值
    int T;
    void insert(vector<string>s, int val){
        int root = 0;
        int sz = s.size();
        for(int i = 0; i < sz; i++){
            //cout << i << "   "<<s[i]<<endl;
            int flg = 0;
            for(int j = 0; j < (int)v[root].size(); j++){
                if(str[v[root][j]] == s[i]){
                    root = v[root][j];
                    flg = 1;
                    break;
                }
            }
            if(!flg){
                //cout << "  bug  "<<tol<<endl;
                v[root].pb(tol);
                str[tol] = s[i];
                if(i==(sz-1)){
                    vis[tol] = 1;
                    vl[tol] = val;
                }
                root = tol++;
            }
        }
        return;
    }
    int size[90000 + 10];
    int readydfs(int root){
        int sz = v[root].size();
        int res = 0;
        if(sz == 0){
            return vl[root];
        }
        int flg = 0;
        //cout << root << "  "<<str[root]<<endl;
        for(int i = 0; i < sz; i++){
            //cout << v[root][i] << "  " << str[v[root][i]]<<endl;
            if(vis[v[root][i]] != 1){
                flg = 1;
            }
            res += readydfs(v[root][i]);
        }
        if(!flg) vis[root] = 2;
    
        return vl[root] = res;
    }
    void dfs(int root, string s, int c){
        int sz = v[root].size();
        int flg = 0;
        if(vis[root]==2)flg = 1;
        if(vl[root] < T){
            if(vis[root] == 2) flg = 1;
            else flg = 2;
        }
        //cout << s <<endl;
        /*if(flg == 0){
            cout<<"- "<<s<<"/ "<<vl[root]<<endl;
        }*/
        if(flg == 1){
            cout <<"  "<<s<<"/ "<<vl[root]<<endl;
            return;
        }
        else if(flg == 2){
            cout << "+ "<<s<<"/ "<<vl[root]<<endl;
            return;
        }
        sort(v[root].begin(), v[root].end(), cmp);
        int vv = 1;
        for(int i = 0; i < sz; i++){
            if(vis[v[root][i]]!=1){
                if(vv &&vl[v[root][i]]>=T){
                    cout<<"- "<<s<<"/ "<<vl[root]<<endl;
                    vv = 0;
                    break;
                }
                //if(flg == 3) dfs(v[root][i], s+"/"+str[v[root][i]], 0);
                
            }
        }
        if(!vv){
            for(int i = 0; i < sz; i++){
                if(vis[v[root][i]]!=1){
                
                    dfs(v[root][i], s+"/"+str[v[root][i]], 0);
                
                }
            }
        }
        else cout << "+ "<<s<<"/ "<<vl[root]<<endl;
        return;
    }
    int main() {
        tol = 1;
        str[0] = "/";
        mem(vis, 0);
        mem(vl, 0);
        ios::sync_with_stdio(false);
        int n;
        cin >> n;
        string tmp;
        for(int t = 1; t <= n; t++){
            int val;
            cin >> tmp >> val;
            int len = tmp.size();
            vector<string>s;
            int p = 0;
            for(int i = 1; i < len; i++){
                if(i && (tmp[i] == '/')){
                    s.pb(tmp.substr(p+1, i-p-1));
                    p = i;
                }
                if(i == len - 1){
                    s.pb(tmp.substr(p+1, i-p));
                }
            }
            insert(s, val);
        }
        readydfs(0);
        /*for(int i = 0; i < tol; i++){
            cout << i <<"  "<<vl[i]<<endl;
        }*/
        cin >> T;
        string s = "";
        dfs(0, s, 0);
        return 0;
    }
  • 相关阅读:
    web前端的面试真题
    web前端面试真题! 面试的经历和回答只做参考1
    web前端面试真题! 面试的经历和回答只做参考
    html面试资料
    angluar.js的核心介绍
    解决 Chrome支持小于12px 的文字
    div居中效果出现的问题和解决方法
    li和li之间的bug解决方法
    前端面试题笔试考题和答案
    html5新增的标签和使用的方法
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9640817.html
Copyright © 2011-2022 走看看