2020 ICPC上海站 M题 Gitignore(map)

2020 ICPC Shanghai Site - M Gitignore

题目链接:https://ac.nowcoder.com/acm/contest/9925/M

比赛的时候,看到这道题,首先想到的还是哈希,然后发现可能会有同名不同级的文件夹,就想用树来实现,结果狠狠的被数据结构给卡了,挺难过的,无情的打铁机器。

事后回来看dalao们的代码,发现用map明明也可以实现,为何自己当时就是想不到,感觉蛮对不起队友的。

不说了,先看题目吧。

题目描述:

Your git project (you don’t need to be familiar with git to solve this problem) has some files that should be ignored from synchronizing. You need to calculate the minimum number of lines needed for gitignore.

Formally, your project is a folder. A folder can have files and sub folders. There are no empty folders (i.e. folders without any files or sub folders inside). Initially, the git software will synchronize all the files in your project. However, you can specify some files and folders in the settings (which is called gitignore) to exclude them from synchronizing. For each line in gitignore, you can specify either a file or all the files in a folder. You can not ignore the whole project folder (i.e. an empty line in gitignore).

You are given paths for all the files in the project and whether they should be ignored or shouldn’t. Your task is to calculate the minimum number of lines for gitignore.

Input
The input contains several test cases. The first line contains a single positive integer T which is the number of test cases. For each test case, you are first given two non-negative numbers n and m. And then n non-empty lines of file paths that should be ignored, and m non-empty lines of file paths that should not be ignored.

The paths are strings containing lower-cased English alphabets and slashes (’/’) only. Slashes are used to separate folders, sub folders and file name. For exapmle, “a/b/c/d” indicates folder “a” in the project folder, folder “b” in folder “a”, folder “c” in “b” and file “d” in folder “c”. All the paths are valid, specifically:

The path is non-empty and it always indicates a file (i.e. the path does not end with a slash).
The path does not start with a slash.
Folder names and file names are non-empty (i.e. there are no consecutive slashes).
File paths are always unique (i.e. all the paths in a test case are different).
In a folder, no sub folders and files share the same names. For example, there won’t be two files “a/b/a” and “a/b/a/d” in one test case. However, files “a/b/a” and “a/b/b” are allowed.
1≤n+m≤100 holds and in the whole input there are no more than 1,000 characters in file paths (i.e. the sum of lengths of file path strings in the whole input file is no more than 1,000).

Output
T lines of non-negative integers, the minimum number of gitignore lines for each test case.

题意:
给你n个字符串,表示n个文件所在的目录,这n个文件想要被忽略,然后给你m个不想被忽略的文件,这m个文件的路径都应该不能被忽略,所以先将需要同步的m个文件路径给同步掉,再遍历n个文件,对于第i个文件,如果遇到上级目录不需要被同步就停止遍历。

代码:

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
 
map<string,int> mp, vis;
int n, m;
void sol(){
	string s[105];
	mp.clear(),vis.clear();
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		cin >> s[i];
	}
	for(int i = 1; i <= m; i++){ //输入m个需要被同步的文件
		string str;
		cin >> str;
		int len = str.length();
		for(int j = 0; j < len; j++){
			if(str[j] == '/'){ //路径上的全需要同步
				mp[str.substr(0,j)] = 1; //substr(l,r) 是string的成员函数,表示从l到r-1的子串				
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; i++){
		bool tag = true;
		int len = s[i].length();
		for(int j = 0; j <= len; j++){
			if(s[i][j] == '/'){
				string tmp = s[i].substr(0,j);
				if(mp[tmp]) tag = true; //如果该目录需要被同步,则需要跳到下级目录查看
				else{
					if(vis[tmp]) tag = 0; //如果该目录不需要同步 但已经被同级目录的文件遍历过了,就不需要再增加次数
					vis[tmp] = 1;
					break;
				}
			}
		}
		ans += (int) tag;
	}
	cout << ans << endl;
}
int main()
{
	int t;
	cin >> t;
	while(t--){
		sol();
	}
	return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值