• 【PAT甲级题解】PAT-2020年冬季考试-甲级


    7-3 File Path

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jshqXAuL-1661926314463)(~/1cb58ce0-f614-4616-b2a2-f18fc3f4fa34.JPG)]

    The figure shows the tree view of directories in Windows File Explorer. When a file is selected, there is a file path shown in the above navigation bar. Now given a tree view of directories, your job is to print the file path for any selected file.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N N N ( ≤ 1 0 3 \le 10^3 103), which is the total number of directories and files. Then N N N lines follow, each gives the unique 4-digit ID of a file or a directory, starting from the unique root ID 0000. The format is that the files of depth d d d will have their IDs indented by d d d spaces. It is guaranteed that there is no conflict in this tree structure.

    Then a positive integer K K K ( ≤ 100 \le 100 100) is given, followed by K K K queries of IDs.

    Output Specification:

    For each queried ID, print in a line the corresponding path from the root to the file in the format: 0000->ID1->ID2->...->ID. If the ID is not in the tree, print Error: ID is not found. instead.

    Sample Input:

    14
    0000
     1234
      2234
       3234
        4234
        4235
        2333
       5234
       6234
        7234
         9999
      0001
       8234
     0002
    4 9999 8234 0002 6666
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Sample Output:

    0000->1234->2234->6234->7234->9999
    0000->1234->0001->8234
    0000->0002
    Error: 6666 is not found.
    
    • 1
    • 2
    • 3
    • 4

    bl[i]存前边有i个空格的最近的。

    #include 
    using namespace std;
    map<string,string>fa;
    map<int,string>bl;
    void dfs(string s)
    {
    	if(s=="0000")
    	{
    		cout<<s;
    		return;
    	}
    	dfs(fa[s]);
    	cout<<"->"<<s;
    }
    int main()
    {
    	
    	fa["0000"]="root";
    	int n;
    	cin>>n;
    	getchar();
    	while(n--)
    	{
    		string s;
    		getline(cin,s);
    		int b=0;
    		while(s[0]==' ')
    		{
    			s.erase(0,1);
    			b++;
    		}
    		if(b!=0)
    		{
    			fa[s]=bl[b-1];
    		}
    		bl[b]=s;
    	}
    	int k;
    	cin>>k;
    	while(k--)
    	{
    		string s;
    		cin>>s;
    		if(fa.find(s)==fa.end())cout<<"Error: "<<s<<" is not found."<<endl;
    		else
    		{
    			dfs(s);
    			cout<<endl;
    		}
    	}
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    7-4 Chemical Equation

    A chemical equation is the symbolic representation of a chemical reaction in the form of symbols and formulae, wherein the reactant entities are given on the left-hand side and the product entities on the right-hand side. For example, C H 4 + 2 O 2 = C O 2 + 2 H 2 O CH_4 + 2 O_2 = CO_2 + 2 H_2 O CH4+2O2=CO2+2H2O means that the reactants in this chemical reaction are methane and oxygen: C H 4 CH_4 CH4 and O 2 O_2 O2, and the products of this reaction are carbon dioxide and water: C O 2 CO_2 CO2 and H 2 O H_2 O H2O.

    Given a set of reactants and products, you are supposed to tell that in which way we can obtain these products, provided that each reactant can be used only once. For the sake of simplicity, we will consider all the entities on the right-hand side of the equation as one single product.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives an integer N N N ( 2 ≤ N ≤ 20 2 \le N \le 20 2N20), followed by N N N distinct indices of reactants. The second line gives an integer M M M ( 1 ≤ M ≤ 10 1 \le M \le 10 1M10), followed by M M M distinct indices of products. The index of an entity is a 2-digit number.

    Then a positive integer K K K ( ≤ 50 \le 50 50) is given, followed by K K K lines of equations, in the format:

    reactant_1 + reactant_2 + ... + reactant_n -> product
    
    • 1

    where all the reactants are distinct and are in increasing order of their indices.

    Note: It is guaranteed that

    • one set of reactants will not produce two or more different products, i.e. situation like 01 + 02 -> 03 and 01 + 02 -> 04 is impossible;
    • a reactant cannot be its product unless it is the only one on the left-hand side, i.e. 01 -> 01 is always true (no matter the equation is given or not), but 01 + 02 -> 01 is impossible; and
    • there are never more than 5 different ways of obtaining a product given in the equations list.

    Output Specification:

    For each case, print the equations that use the given reactants to obtain all the given products. Note that each reactant can be used only once.

    Each equation occupies a line, in the same format as we see in the inputs. The equations must be print in the same order as the products given in the input. For each product in order, if the solution is not unique, always print the one with the smallest sequence of reactants – A sequence { a 1 , ⋯   , a m a_1, \cdots , a_m a1,,am } is said to be smaller than another sequence { b 1 , ⋯   , b n b_1, \cdots , b_n b1,,bn } if there exists 1 ≤ i ≤ m i n ( m , n ) 1\le i \le min(m,n) 1imin(m,n) so that a j = b j a_j=b_j aj=bj for all j < i jj<i, and a i < b i a_iai<bi.

    It is guaranteed that at least one solution exists.

    Sample Input:

    8 09 05 03 04 02 01 16 10
    3 08 03 04
    6
    03 + 09 -> 08
    02 + 08 -> 04
    02 + 04 -> 03
    01 + 05 -> 03
    01 + 09 + 16 -> 03
    02 + 03 + 05 -> 08
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Sample Output:

    02 + 03 + 05 -> 08
    01 + 09 + 16 -> 03
    04 -> 04
    
    • 1
    • 2
    • 3

    对每个产物的化学式进行排序,直接dfs暴力。

    #include 
    using namespace std;
    int n,m,k,a[105],b[15],vis[105];
    struct s{
        int n=0;
        vector<int>v[55];
        int r;
    }s[105];
    void dfs(int idx)
    {
    	if(idx==m)
    	{
    		for(int i=0;i<m;i++)
    		{
    			int x=b[i];
    			for(int j=0;j<s[x].v[s[x].r].size();j++)
    			{
    				if(j!=0)cout<<"+ ";
    				printf("%02d ",s[x].v[s[x].r][j]);
    			}
    			printf("-> %02d\n",x);
    		}
    		exit(0);
    	}
    	int x=b[idx];
    	for(int i=0;i<s[x].n;i++)
    	{
    		for(int j=0;j<s[x].v[i].size();j++)
    		{
    			if(vis[s[x].v[i][j]]==1||a[s[x].v[i][j]]==0)
    			{
    				goto gt;
    			}
    		}
    		for(int j=0;j<s[x].v[i].size();j++)
    		{
    			vis[s[x].v[i][j]]=1;
    		}
    		s[x].r=i;
    		dfs(idx+1);
    		for(int j=0;j<s[x].v[i].size();j++)
    		{
    			vis[s[x].v[i][j]]=0;
    		}
    		gt:;
    	}
    }
    int main()
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
        	int x;
    		cin>>x;
    		a[x]++; 
    	}
        cin>>m;
        for(int i=0;i<m;i++)cin>>b[i];
        cin>>k;
        getchar(); 
        while(k--)
        {
            string str,str2;
            getline(cin,str);
            stringstream ss;
            ss<<str;
            vector<int>vec;
            int f=0,p;
            while(ss)
            {
                ss>>str2;
                if(f==1)
                {
                    p=stoi(str2);
                }
                else if(str2=="->")
                {
                    f=1;
                }
                else if(str2!="+")
                {
                    vec.push_back(stoi(str2));
                }
            }
            s[p].v[s[p].n++]=vec;
        }
        for(int i=0;i<m;i++)
        {
        	s[b[i]].v[s[b[i]].n++].push_back(b[i]);
        	sort(s[b[i]].v,s[b[i]].v+s[b[i]].n);
    	}
        dfs(0);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
  • 相关阅读:
    Go--运算符
    8万字208道Java经典面试题总结(附答案)
    RTPS协议之Behavior Module
    zookeeper实现分布式锁
    node 第十二天 npm补充 详解package-lock.json在团队协作中的作用
    服务器远程桌面无法连接
    【Maven】maven安装、IDEA创建maven管理的web项目、添加依赖、集成Tomcat
    第L6周:机器学习-随机森林(RF)
    DLL远程线程注入技术
    Containerd 安装使用与高级命令行工具 crictl、nerdctl
  • 原文地址:https://blog.csdn.net/l503301397/article/details/126622904