题解

58 条题解

  • 1
    @ 2022-07-19 18:06:08
    #include <iostream>
    #include <string>
     using namespace std;
    string stack[300], aim;
    int top;
    void solution(string tmp)
    {
        int branch = top;
        if(tmp.find('.') == string::npos)
        {
            stack[top++] = tmp;
            int num;
            string tmp1;
            cin >> num;
            for(int i = 0; i < num; i++)
            {
                cin >> tmp1;
                solution(tmp1);
            }
        }
        else 
        {
            stack[top++] = tmp;
            if(tmp == aim)
            {
                for(int i = 0; i < top - 1; i++)
                    cout << stack[i] << '\\';
                cout << stack[top-1] << endl;
            }
        }
        top = branch;
    }
    int main()
    {
        cin >> aim;
        string tmp;
        while(cin >> tmp)
            solution(tmp);
        return 0;
    }
    
    
  • 0
    @ 2020-06-13 19:22:07

    多叉树带父节点
    ```cpp
    #include<iostream>
    #include<vector>
    #include<stack>
    using namespace std;
    class Tree {
    public:
    bool file;
    string name;
    int children_num=0;
    vector<Tree*> children;
    Tree* father=nullptr;
    Tree(bool file,string name,Tree *father) {
    this->name = name;
    this->file = file;
    this->father = father;
    }
    };
    void find(Tree node,string keyword) {
    stack<string> path;
    while (node!=nullptr){
    path.push(node->name);
    node = node->father;
    }
    while (!path.empty()){
    cout << path.top();
    path.pop();
    }
    cout << endl;
    }
    Tree
    new_dir(string name,int child_num,Tree* father,string keyword) {
    Tree* tree = new(Tree)(false, name+"\",father);
    tree->children_num = child_num;

    for (int i = 0; i < child_num; i++) {
    string child_name;
    cin >> child_name;
    if (child_name.find(".")==name.npos) {
    int num;
    cin >> num;
    tree->children.push_back(new_dir(child_name, num, tree,keyword));
    }
    else {
    Tree* child = new(Tree)(true,child_name,tree);
    tree->children.push_back(child);
    if (child->name == keyword) {
    find(child, keyword);
    }
    }
    }
    return tree;
    }

    int main() {
    string keyword;
    cin >> keyword;
    Tree* root = new(Tree)(false, "", nullptr);
    string name;
    while (cin>>name){
    if (name.find(".")!=name.npos) {
    Tree* tree = new(Tree)(true, name, root);
    root->children.push_back(tree);
    if (tree->name == keyword) {
    find(tree, keyword);
    }
    }
    else {
    int num;
    cin >> num;
    Tree* tree = new_dir(name,num,root,keyword);
    root->children.push_back(tree);
    }
    root->children_num++;
    }
    return 0;
    }
    ```

  • 0
    @ 2018-08-11 19:29:37

    递归大法好

    #include<iostream>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #include<vector>
    #define N 2000
    using namespace std;
    
    void in(int &x){
        register char c=getchar();x=0;int f=1;
        while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
        while(isdigit(c)){x=x*10+c-'0';c=getchar();}
        x*=f;
    }
    
    string an,s,ans[N];
    string S[N];
    bool flg=0;
    void dg(int cnt){
        s="";
        while(cin>>s) break;
        if(s=="") {flg=1;return;}
        S[cnt]=s;
        int l=s.length();
        if(s.find('.')!=string::npos){
            if(s==an){
                for(int i=1;i<=cnt-1;i++) cout<<S[i]<<"\\";
                cout<<an<<"\n";
            }
        }
        else {
            int p;
            cin>>p;
            for(int i=1;i<=p;i++)
                dg(cnt+1);
        }return;
    }
    int main()
    {
        cin>>an;
        while(1){
            dg(1);
            if(flg) break;
        }return 0;
    }
    
  • 0
    @ 2018-08-10 15:21:19

    按照文件夹样式建图模拟,最后查询即可。
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <map>
    using namespace std;
    #define N int(3e3+2)
    #define M int(1e5+2)
    int top,stack[N],belong[N],sck[N],sum,p;
    map <int,string> f;
    string s,t;
    struct ahah{
    int nxt,to;
    }edge[N];
    int head[N],tot;
    void add(int x,int y)
    {
    edge[++tot].nxt=head[x],edge[tot].to=y,head[x]=tot;
    }
    void dfs(int s)
    {
    if(s==0)return ;
    stack[++top]=s;
    for(int i=head[s];i;i=edge[i].nxt)
    dfs(edge[i].to);
    }
    int main()
    {
    cin>>s;
    while(cin>>t)
    {
    while(top&&stack[top]==0)top--,sum--;
    f[++p]=t;
    if(t.find(".")==-1)
    {
    stack[top]--;
    add(p,sck[sum]);
    scanf("%d",&stack[++top]);
    sck[++sum]=p;
    continue;
    }
    stack[top]--;
    add(p,sck[sum]);
    }
    for(int i=1;i<=p;i++)
    {
    if(f[i]==s)
    {
    dfs(i);
    cout<<f[stack[top]];
    for(int i=top-1;i>=1;i--)if(stack[i]!=0)cout<<"\"<<f[stack[i]];
    top=0;
    printf("\n");
    }
    }
    }

  • 0
    @ 2017-08-31 11:52:55
    //弱弱的递归,用branch记录上一个位置
    #include <iostream>
    #include <string>
     using namespace std;
    string stack[300], aim;
    int top;
    void solution(string tmp)
    {
        int branch = top;
        if(tmp.find('.') == string::npos)
        {
            stack[top++] = tmp;
            int num;
            string tmp1;
            cin >> num;
            for(int i = 0; i < num; i++)
            {
                cin >> tmp1;
                solution(tmp1);
            }
        }
        else 
        {
            stack[top++] = tmp;
            if(tmp == aim)
            {
                for(int i = 0; i < top - 1; i++)
                    cout << stack[i] << '\\';
                cout << stack[top-1] << endl;
            }
        }
        top = branch;
    }
    int main()
    {
        cin >> aim;
        string tmp;
        while(cin >> tmp)
            solution(tmp);
        return 0;
    }
    
  • 0
    @ 2016-08-21 16:28:13

    弱数据,直接递归即可AC
    ```c++
    #include <bits/stdc++.h>
    using namespace std;

    string str;

    bool work(const string &pos)
    {
    string ths;
    if (!(cin >> ths)) return 0;
    if (ths.find('.') == string::npos) {
    int d; cin >> d;
    for (int i = 1; i <= d; i++)
    if(!work(pos+ths+'\'))
    throw;
    } else {
    if (ths == str)
    puts((pos+ths).c_str());
    }
    return 1;
    }

    int main()
    {
    cin >> str;
    while (work(""));
    return 0;
    }
    ```

  • 0
    @ 2012-11-07 20:01:27

    编译通过...

    ├ 测试数据 01:答案正确... (12ms, 1536KB)

    ├ 测试数据 02:答案正确... (0ms, 1536KB)

    ├ 测试数据 03:答案正确... (0ms, 1536KB)

    ├ 测试数据 04:答案正确... (0ms, 1536KB)

    ├ 测试数据 05:答案正确... (0ms, 1536KB)

    ├ 测试数据 06:答案正确... (123ms, 1536KB)

    ├ 测试数据 07:答案正确... (124ms, 1536KB)

    ├ 测试数据 08:答案正确... (124ms, 1536KB)

    ├ 测试数据 09:答案正确... (218ms, 1536KB)

    ├ 测试数据 10:答案正确... (137ms, 1536KB)

    ---|---|---|---|---|---|---|---|-

    Accepted / 100 / 742ms / 1536KB

    递归就可以了

    在自己的FP上测样例的时候,文件读完但是必须手动跳出

    忐忑的提交上去结果竟然过了……

  • 0
    @ 2009-11-08 11:54:36

    100AC!

    开始写了60行超时了 .. 后来写了个20行的递归就过了

  • 0
    @ 2009-11-06 21:15:49

    建议楼下在递归中用ansistring的,当心栈溢出

    还有,郁闷的是,题目说过每行字符不超过25的,结果数据都是255 = =!!!

  • 0
    @ 2009-10-23 22:57:43

    ansistring和string的差别就是50分!!

  • 0
    @ 2009-10-22 17:39:53

    第一反应是非递归写法。。。

    很容易知道,只需要维护一个栈,当且仅当读入的为文件时,减少当前元素的儿子数,读入的为文件夹时,增加当前的元素个数。

    非递归的精髓在于pop的过程,以下贴出这段过程:

    procedure pop(var p:longint); //p指针指空

    begin

    dec(stack[p-1].s);

    while stack[p-1].s=0 do

    begin

    dec(p);

    dec(stack[p-1].s);

    end;

    end;

  • 0
    @ 2009-10-08 12:43:18

    右斜杠打成左斜杠了…………囧

  • 0
    @ 2009-10-07 21:14:27

    这种题一次AC。

    我太崇拜我自己了。

  • 0
    @ 2009-10-06 21:36:42

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    晕 if 与 while 打错了 白交了n次

  • 0
    @ 2009-09-19 15:32:18

    悲剧啊..

    太久没写递归了

  • 0
    @ 2009-09-17 21:06:03

    卧槽, 超过25的啊- - 槽死

  • 0
    @ 2009-09-15 13:01:18

    这 是 为 什 么 呢-3- 

      谁 告 诉 我 应 该 怎 么 读 入-3- 

      

      谢 谢 拉0 0 

  • 0
    @ 2009-09-15 12:36:15

    没有一次AC....

    我没用大牛说的递归啊,直接用WHILE循环弄的。

    此题很水。

    注意的是判断文件夹是否读完不能用IF,要用WHILE,我就是因为这个才过了50分....

  • 0
    @ 2009-09-14 20:43:47

    '/' '\'大反了

  • 0
    @ 2009-09-12 23:03:21

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    呜呜呜 没一次AC

信息

ID
1622
难度
5
分类
模拟 | 数据结构 | 点击显示
标签
递交数
1826
已通过
613
通过率
34%
被复制
3
上传者