【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

Sample Output:

0000->1234->2234->6234->7234->9999
0000->1234->0001->8234
0000->0002
Error: 6666 is not found.

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

#include <bits/stdc++.h>
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;
		}
	}
} 

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

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 j<i j<i, and a i < b i a_i<b_i ai<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

Sample Output:

02 + 03 + 05 -> 08
01 + 09 + 16 -> 03
04 -> 04

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

#include <bits/stdc++.h>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小柳学渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值