Hihocode-统计子目录

题目名称:统计子目录
题目链接:统计子目录
描述

小Hi的电脑的文件系统中一共有N个文件,例如:

/hihocoder/offer22/solutions/p1

/hihocoder/challenge30/p1/test

/game/moba/dota2/uninstall

小Hi想统计其中一共有多少个不同的子目录。上例中一共有8个不同的子目录:

/hihocoder

/hihocoder/offer22

/hihocoder/offer22/solutions

/hihocoder/challenge30

/hihocoder/challenge30/p1

/game

/game/moba

/game/moba/dota2/

输入

第一行包含一个整数N (1 ≤ N ≤ 10000)

以下N行每行包含一个字符串,代表一个文件的绝对路径。保证路径从根目录"/"开始,并且文件名和目录名只包含小写字母和数字。

对于80%的数据,N个文件的绝对路径长度之和不超过10000

对于100%的数据,N个文件的绝对路径长度之和不超过500000

输出

一个整数代表不同子目录的数目。

样例输入

3
/hihocoder/offer22/solutions/p1
/hihocoder/challenge30/p1/test
/game/moba/dota2/uninstall

样例输出

8

解题思路

一个简单的字典树练习,稍加修改即可

完整代码
#include<bits/stdc++.h>

using namespace std;
#define N 500010
string str;
struct Node{
	int childs[37];
}tire[N];

int indx,n,ans; //下标,输入个数 
int getIndex(char ch){
	if(ch=='/') return 36;
	else if('0'<=ch&&ch<='9') return ch-'0';
	else return ch-'a'+10;
} 

void insert(){
	int cnt=0;
	for(int i=0;i<str.size();i++){
		int child=getIndex(str[i]);
		if(tire[cnt].childs[child]==0){
			tire[cnt].childs[child]=++indx;
			ans+=(child==36)?1:0;
		}
		cnt=tire[cnt].childs[child];
	}
}

int main()
{
	cin>>n;
	ans=0;
	while(n--){
		cin>>str;
		insert();
	}
	if(tire[0].childs[36]) ans--;
	cout<<ans<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值