P9831 [ICPC2020 Shanghai R] Gitignore 题解

一道比较有思维的模拟题,首先贪心地考虑,如果一个文件夹可以直接整个删除,那么就没必要把里面的子文件一个一个删除。

那么可以考虑使用 map 将所有不能被删的标记出来,再依次将每个串从外层到内层处理,如果可以删就直接删去,并且打上打上标记避免重复删。

#include
using namespace std;
int T;
int n,m;
const int maxn = 105;
string s[maxn];
map mp,vis;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    cin>>T;
    while(T --> 0){
        mp.clear();vis.clear();
        cin>>n>>m;
        for(int i(1);i <= n;++i){
            cin>>s[i];//can be ignored
        }
        for(int i(1);i <= m;++i){
            string str,tmp = "";
            cin>>str; register int len = str.length();
            for(int j(0);j < len;++j){
                if(str[j] == '/'){
                    mp[tmp] = 1;
                }
                tmp += str[j];
            }
        }
        int ans = 0;
        for(int i(1);i <= n;++i){
            bool f = 1; string tmp = "";
            register int len = s[i].length();
            for(int j(0);j < len;++j){
                if(s[i][j] == '/'){
                    if(mp[tmp]) f = 1;
                    else{
                        if(vis[tmp]) f = 0;
                        vis[tmp] = 1;
                        break;
                    }
                }
                tmp += s[i][j];
            }
            ans += int(f);
        }
        cout<     }
    return 0;
}

你可能感兴趣的:(算法,动态规划,c++,深度优先,数据结构)