第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(上海)M . Gitignore(模拟)
链接:https://ac.nowcoder.com/acm/contest/9925/M
来源:牛客网
题目描述
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.
输入描述:
The input contains several test cases. The first line contains a single positive integer TT which is the number of test cases. For each test case, you are first given two non-negative numbers nn and mm. And then nn non-empty lines of file paths that should be ignored, and mm 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:
1. The path is non-empty and it always indicates a file (i.e. the path does not end with a slash).
2. The path does not start with a slash.
3. Folder names and file names are non-empty (i.e. there are no consecutive slashes).
4. File paths are always unique (i.e. all the paths in a test case are different).
5. 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≤1001≤n+m≤100 holds and in the whole input there are no more than 1,0001,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,0001,000).
输出描述:
TT lines of non-negative integers, the minimum number of gitignore lines for each test case.
示例1
输入
复制
2
3 0
data/train
data/test
model
3 1
data/train
data/test
model
data/sample
输出
复制
2
3
说明
In the first sample test case, the corresponding gitignore file contains 2 lines: a folder line "data/" and a file name "model".
In the second sample test case, the corresponding gitignore file contains 3 file lines: "data/train", "data/test" and "model".
模拟。首先不能ignore的文件的路径上的所有文件夹都是不能ignore的,然后从能ignore的文件夹里找最少的能覆盖所有需要ignore的文件的文件夹。
一开始仿照文件树的思路,先是想到了字典树但感觉写起来很麻烦,然后尝试把每个文件夹/文件字符串用map映射成节点,然后建树跑类似树dp的过程,但t了...只能过40%左右的点(看别人代码貌似也有这么写的就能过,晕了)。
最后还是学大佬写法用set一通瞎搞...
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
using namespace std;
int n, m;
int main()
{
freopen("data.txt", "r", stdin);
int t;
cin >> t;
while(t--)
{
int ans = 0;
cin >> n >> m;
vector<string> v;
set<string> s1, s2;
for(int i = 1; i <= n; i++)
{
string tmp;
cin >> tmp;
v.push_back(tmp);
}
for(int i = 1; i <= m; i++)
{
string tmp;
cin >> tmp;
for(int j = 1; j < tmp.size(); j++)
{
if(tmp[j] == '/')
{
s1.insert(tmp.substr(0, j));//每一个子文件夹都要标记
}
}
}
for(int i = 0; i < v.size(); i++)
{
bool pd = 1;
for(int j = 0; j < v[i].size(); j++)
{
if(v[i][j] == '/')
{
string tmp = v[i].substr(0, j);
if(s1.find(tmp) != s1.end()) continue;//不能直接ignore当前文件夹
else if(s2.find(tmp) != s2.end())//当前文件夹在已经ignore的文件夹set里出现过了,直接忽略
{
pd = 0;
break;
}
else//插入
{
pd = 0;
ans++;
s2.insert(tmp);
break;
}
}
}
if (pd && !s2.count(v[i]) && !s1.count(v[i])) ans++;//单个文件
}
cout << ans << endl;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!