题解:P13393 [GCJ 2010 #1B] File Fix-it

· · 题解

题目:P13393 [GCJ 2010 #1B] File Fix-it

解题思路

简单解释一下。

/home/gcj/finals

上面的例子访问了 3 种路径分别是:

  1. /home
  2. /home/gcj
  3. /home/gcj/finals

所以我们可以通过维护一个 map ,将每个完整的目录通过 map 标记这个路径是否出现过,记录当前的路径层数

代码:

#include<bits/stdc++.h>
using namespace std;
int T,n,m;
int ans;
string str;

void work(){
    //初始化 
    map<string,int>mp;//定义map,当前目录的层数 
    int cnt;
    string news="";
    ans=0;

    cin>>n>>m;

    //已有路径 
    for(int i=1;i<=n;i++){
        cnt=0;
        news="";//存完整的路径 
        cin>>str;
        str=str+'/';//加 '/' 以 便于 访问最后以为路径完整 
        for(int j=0;j<=str.size();j++){
            if(str[j]=='/'){//下一位是 '/' 进行路径记录 
                cnt++;
                if(news!=""){
                    if(mp[news]!=cnt){
                        mp[news]=cnt;
                    }
                }
            }   
            news=news+str[j];
        }
    }

    //新路径 
    for(int i=1;i<=m;i++){
        cnt=0;
        news="";
        cin>>str;
        str=str+'/';
        for(int j=0;j<=str.size();j++){
            if(str[j]=='/'){
                cnt++;
                if(news!=""){
                    if(mp[news]!=cnt){
                        mp[news]=cnt;
                        ans++;//不同路径,结果加1 
                    }
                }
            }
            news=news+str[j];
        }
    }
    return ;
}

int main(){
    cin>>T;
    int k=0;//编号 
    while(T--){
        work();
        k++;
        printf("Case #%d: %d\n",k,ans);
    }
    return 0;
}