zoukankan      html  css  js  c++  java
  • 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(上海)M Gitignore

    题目

    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.

    在这里插入图片描述
    在这里插入图片描述

    题意

    给你n个类似于 a/b/c/d 这样的文件夹或者文件,定义为gitignore,即可以被忽略的目录,m个这样的目录,表示不能被ignore的目录。如果一个文件夹里面不是所有的文件都能被忽略的话就不能忽略这个文件夹,否则可以直接忽略这个文件夹。
    问最后gitignore最简用几行可以表达 eg:
    3 0
    data/train
    data/test
    model

    data/train 和 data/test 都可以被忽略 所以就可以用忽略 data/ 来表示忽略了这个文件夹

    3 1
    data/train
    data/test
    model
    data/sample
    由于data/sample不能被忽略,所以在写gitignore的时候就需要把
    data/train
    data/test
    model 都写上

    思路

    1.设置答案初始为n,如果有可以直接忽略的就ans-1
    2.先把m个不能被直接忽略的目录打上标记mp[now] = 1 ,在1-n找的时候直接continue即可。
    3.对于一整个文件夹都可以被忽略的情况,如果第一次遇到,就把mp[now] 设为2 表示当前这个s就代表一整个文件夹(因为不能忽略一整个文件夹,所以必须保留一个)
    4.遇到mp[now]=2 的 就说明已经被忽略了,并且有一个当作整个被忽略的文件夹,ans-1

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 100+10;
    
    map<string,int>mp;
    string a[maxn],b[maxn];
    void Solve()
    {
    	mp.clear();
    	int n,m;
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++) cin>>a[i];
    	for(int i=1;i<=m;i++) cin>>b[i];
    	
    	int ans = n; // 初始化答案为 n 
    	for(int i=1;i<=m;i++)
    	{
    		int len = b[i].size();
    		string now = "";
    		for(int j=0;j<len;j++)
    		{
    			now += b[i][j];
    			if(b[i][j] == '/')
    			{
    				mp[now] = 1; // 当前父目录里面有不能ignore的 
    			}
    		}
    	}
    	
    	for(int i=1;i<=n;i++)
    	{
    		int len = a[i].size();
    		string now = "";
    		for(int j=0;j<len;j++)
    		{
    			now += a[i][j];
    			if(a[i][j] == '/')
    			{
    				if(mp[now] == 1) continue;
    				else if(mp[now] == 0) mp[now] = 2; // 如果当前父目录是可以忽略,就把当前这个当作忽略后的父节点,因为至少有一个
    				else if(mp[now] == 2) // 如果已经有用来表示ignore之后的文件夹父节点了,答案-1,即忽略当前的
    				{
    					ans --;
    					break;
    				} 
    			}
    		}
    	}
    	printf("%d",ans);
    }
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		Solve();
    		if(t) puts("");
    	}
    } 
    
  • 相关阅读:
    WP8日历(含农历)APP
    NHibernate3剖析:Mapping篇之集合映射基础(2):Bag映射
    初探springmvc
    树的子结构
    Java内存分析
    java8_api_misc
    iOS开发多线程篇 09 —NSOperation简单介绍
    CALayer1-简介
    NSCharacterSet
    iOS 音频开发
  • 原文地址:https://www.cnblogs.com/liangyj/p/14195183.html
Copyright © 2011-2022 走看看