Codeforces 66C Petya and File System(STL--map)

Codeforces 66C Petya and File System

链接:http://codeforces.com/problemset/problem/66/C


题目

Time Limit:3000MS Memory Limit:262144KB
Description
Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders).

More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder.

For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples.

You are given a few files that Petya has managed to create. The path to each file looks as follows:

diskName:\folder1\folder2…\foldern\fileName

  • diskName is single capital letter from the set {C,D,E,F,G}.
  • folder1, …, foldern are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (n ≥ 1)
  • fileName is a file name in the form of name.extension, where the name and the extension are nonempty sequences of lowercase Latin letters and digits from 0 to 9.
    It is also known that there is no file whose path looks like diskName:\fileName. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder.

Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders.

Input
Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once.
There is at least one line in the input data.

Output
Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders.

Sample Input
C:\folder1\file1.txt
Sample Output
0 1

Sample Input
C:\folder1\folder2\folder3\file1.txt
C:\folder1\folder2\folder4\file1.txt
D:\folder1\file1.txt
Sample Output
3 2

Sample Input
C:\file\file\file\file\file.txt
C:\file\file\file\file2\file.txt
Sample Output
4 2

分析

一道考察STL应用的题,可以使用map函数来解。
本代码在处理时进行了一部分优化。从后向前查找遇到的第一个’\’并切割,从而获取了每一段文件夹路径,然后用map记录。同时从后向前更新的过程中使用cntFiles变量存储子文件的个数,并不断向上更新。当字段只剩下盘符时,退出。


源码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,1,sizeof x)
#define mem11(x) memset(x,-1,sizeof x)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int MAXN = 1e6+10;
const int MOD = 1000000007;

int cntSF;
string s;
map<string, int> subF;
map<string, int> files;
int maxa,maxb;

int getPos(){
    int len = s.length();
    int p = len-1;
    while(p>=0){
        if(s[p] == '\\')
            return p;
        else
            p--;
    }
}

int main(){
    maxa = maxb = -INF;
    subF.clear();
    files.clear();
    while(cin >> s){
        int cntFiles = 0;
        bool appeared;
        while(1){
            int pos = getPos();
            if(pos == 2)
                break;
            else{
                s = s.substr(0,pos);
                appeared = subF[s];
                files[s] += cntFiles;
                subF[s]++;
                maxa = max(maxa, files[s]);
                maxb = max(maxb, subF[s]);
                if(!appeared)
                    cntFiles++;
            }
        }
    }
    cout << maxa << " " << maxb << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值