![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jshqXAuL-1661926314463)(~/1cb58ce0-f614-4616-b2a2-f18fc3f4fa34.JPG)]](https://1000bd.com/contentImg/2023/10/27/122418700.png)
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.
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.
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.
14
0000
1234
2234
3234
4234
4235
2333
5234
6234
7234
9999
0001
8234
0002
4 9999 8234 0002 6666
0000->1234->2234->6234->7234->9999
0000->1234->0001->8234
0000->0002
Error: 6666 is not found.
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;
}
}
}
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.
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 2≤N≤20), 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 1≤M≤10), 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
01 + 02 -> 03 and 01 + 02 -> 04 is impossible;01 -> 01 is always true (no matter the equation is given or not), but 01 + 02 -> 01 is impossible; andFor 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)
1≤i≤min(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_i
It is guaranteed that at least one solution exists.
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
02 + 03 + 05 -> 08
01 + 09 + 16 -> 03
04 -> 04
对每个产物的化学式进行排序,直接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;
}