持续创作,加速成长!这是我参与「掘金日新计划 · 1 月更文挑战」的第16天,点击查看活动详情
第 45 届ICPC区域赛(上海)M - Gitignore
题目连接
链接:ac.nowcoder.com/acm/contest…
来源:牛客网
题目描述
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.
题目大意
给出 个文件路径,需要被删除。
给出 个文件路径,需要被保留,
求最少需要执行多少次删除文件夹或删除文件操作,删掉一个文件夹将会把其中的文件和子文件夹都删掉。
思路
把需要删除的节点加入 trie 树,对于每个删除的文件,把其经过的 trie 树上的节点打上标记。
遍历 trie 树,如果当前节点没有标记,就执行删除操作,否则递归其所有的子文件夹和文件,判断有无标记。
代码
#include <bits/stdc++.h>
using namespace std;
int m,n,len,tot,ans;
string s,o;
map<string,int> mp[500001];
int flag[500001],cnt[500001];
void dfs(int t)
{
if (flag[t]==0)
{
ans++;
return;
}
for (auto v:mp[t])
{
dfs(v.second);
}
}
int solve()
{
for (int i=0;i<=tot;++i)
{
mp[i].clear();
flag[i]=cnt[i]=0;
}
scanf("%d%d",&n,&m);
tot=0;
flag[0]=1;
for (int i=1;i<=n;++i)
{
cin>>s;
s+='/';
len=s.size();
string x;
for (int j=0,g=0;j<len;++j)
{
if (s[j]=='/')
{
if (mp[g].count(x)==0) mp[g][x]=++tot;
g=mp[g][x];
cnt[g]++;
x=o;
}
else x+=s[j];
}
}
for (int i=1;i<=m;++i)
{
cin>>s;
s+='/';
len=s.size();
string x;
for (int j=0,g=0;j<len;++j)
{
if (s[j]=='/')
{
if (mp[g].count(x)==0) break;
g=mp[g][x];
flag[g]=1;
x=o;
}
else x+=s[j];
}
}
ans=0;
dfs(0);
return ans;
}
int main()
{
int T;
for (scanf("%d",&T);T--;)
{
printf("%d\n",solve());
}
return 0;
}