「LuoguP1738」洛谷的文件夹

LuoguP1738 洛谷的文件夹

题目描述

kkksc03是个非凡的空想家!在短时间内他设想了大量网页,然后总是交给可怜的lzn去实现。

洛谷的网页端,有很多文件夹,文件夹还套着文件夹。

例如:/luogu/application/controller表示根目录下有一个名称为luogu的文件夹,这个文件夹下有一个名称application的文件夹,其中还有名为controller的文件夹。

每个路径的第1个字符总是’/’,且没有两个连续的’/’,最后的字符不是’/’。所有名称仅包含数字和小写字母。

目前根目录是空的。kkksc03想好了很多应该有的文件夹路径名。问题是,需要是使这些文件夹都存在,需要新建几个文件夹呢?

输入输出格式

输入格式

输入文件第1行为一个正整数N。

接下来N行,每行为一个描述路径的字符串,长度均不超过100。

输出格式

输出应包含N行,每行1个正整数,第i行输出若要使第1个路径到第i个路径存在,最少需要新建多少个文件夹。

INPUT & OUTPUT‘s examples

Input's examples #1

2
/luogu/application/controller
/luogu/application/view

Output's examples #1

3
4

分析

题目要求的是需要多少个文件夹。

我们直接把给定字符串中的目录名称截下来,然后丢进set里,询问的时候直接输出set.size()即可。

但,

这样做是不对的。

一个简单的HACK:

INPUT:

1
/wyh/txdy/txdy

显然能够看出来,答案应该是3,而上面的算法将会输出2。

所以,我们还是模拟题意,从题目给定的根目录开始,如果遇到一个文件夹,能在set里找到就再判下一层。

找不到就新建文件夹,丢进set里。

代码

/* Headers */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<climits>
#include<iostream>
#include<map>
#include<set>
#define FOR(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define ROF(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define FORL(i,a,b,c) for(long long i=(a);i<=(b);i+=(c))
#define ROFL(i,a,b,c) for(long long i=(a);i>=(b);i-=(c))
#define FORR(i,a,b,c) for(register int i=(a);i<=(b);i+=(c))
#define ROFR(i,a,b,c) for(register int i=(a);i>=(b);i-=(c))
#define lowbit(x) x&(-x)
#define LeftChild(x) x<<1
#define RightChild(x) (x<<1)+1
#define RevEdge(x) x^1
#define FILE_IN(x) freopen(x,"r",stdin);
#define FILE_OUT(x) freopen(x,"w",stdout);
#define CLOSE_IN() fclose(stdin);
#define CLOSE_OUT() fclose(stdout);
#define IOS(x) std::ios::sync_with_stdio(x)
#define Dividing() printf("-----------------------------------\n");
namespace FastIO{
    const int BUFSIZE = 1 << 20;
    char ibuf[BUFSIZE],*is = ibuf,*its = ibuf;
    char obuf[BUFSIZE],*os = obuf,*ot = obuf + BUFSIZE;
    inline char getch(){
        if(is == its)
            its = (is = ibuf)+fread(ibuf,1,BUFSIZE,stdin);
        return (is == its)?EOF:*is++;
    }
    inline int getint(){
        int res = 0,neg = 0,ch = getch();
        while(!(isdigit(ch) || ch == '-') && ch != EOF)
            ch = getch();
        if(ch == '-'){
            neg = 1;ch = getch();
        }
        while(isdigit(ch)){
            res = (res << 3) + (res << 1)+ (ch - '0');
            ch = getch();
        }
        return neg?-res:res;
    }
    inline void flush(){
        fwrite(obuf,1,os-obuf,stdout);
        os = obuf;
    }
    inline void putch(char ch){
        *os++ = ch;
        if(os == ot)    flush();
    }
    inline void putint(int res){
        static char q[10];
        if(res==0)  putch('0');
        else if(res < 0){putch('-');res = -res;}
        int top = 0;
        while(res){
            q[top++] = res % 10 + '0';
            res /= 10;
        }
        while(top--)    putch(q[top]);
    }
    inline void space(bool x){
        if(!x) putch('\n');
        else putch(' ');
    }
}
inline void read(int &x){
    int rt = FastIO::getint();
    x = rt;
}
inline void print(int x,bool enter){
    FastIO::putint(x);
    FastIO::flush();
    FastIO::space(enter);
}
/* definitions */
std::string str,New;
std::set<std::string> s;
int n;
int tmp,last;
/* functions */
int main(int argc,char *argv[]){
    std::cin>>n;
    FOR(i,1,n,1){
        std::cin>>str;
        New = "";
        for(auto j : str){
            if(j == '/') s.insert(New);
            New += j;
        }
        s.insert(New);
        std::cout<<s.size() - 1 << std::endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/herself32-lyoi/p/10803200.html