PAT | 蓝桥 | LeetCode学习路径 & 刷题经验

你们要不要考虑也节省一下时间~

这份PDF题目叫做《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验 by 柳婼》,希望可以帮助学习算法路上的小可爱们节约时间、少踩一些坑、多走一些捷径~

一年前看过我blog的人应该知道,我曾经开通过知乎的值乎付费咨询,大概开通了大半年时间,期间也收到了很多咨询,绝大多数提问的话题都是“PAT、蓝桥、LeetCode怎么学如何刷题”,我也一一认真做了回答(绝大多数回答都在半小时以上),但是值乎的回答只能够发布语音,而且有回答时效,提问也有字数限制,后来问的人越来越多,我一天要花数小时在知乎回答上,而且回答的都是几乎相同的问题…对于分享算法经验来说,短短半小时确实不够,很多观点无法详细解释缘由,值乎一对一咨询确实不是一个很好的分享算法经验的平台,听者也很难短时间形成对问题答案的清晰理解,所以后来半年前我就关闭了知乎咨询的功能~不过还是有很多小可爱通过各种渠道向我咨询经验等问题,所以我花了好多天时间将这些问题的回答、刷题笔记、经验全都整理在了一份PDF中~这些问题包括:

里面不仅有关于这些竞赛的介绍、应该看哪些书、如何刷题,还有我自己刷算法过程中整理的笔记,目录如下:

这份经验一共3万7千字(想当年800字的高考作文都写那么辛苦…)在算法路上,我能帮的只有这么多了…希望能帮助你们少走很多弯路,少踩很多坑~剩下的就是靠你们自己刷题啦~还和离线版订阅获取的方式一样…打赏29元并备注邮箱号即可…打赏二维码在最下面…24小时内发送到邮箱…(一般一两个小时就收到了~)时间仓促,后期还会对这份文档的内容进行扩充完善…到时候有版本更新还会继续发送到邮箱中~感谢支持与信任~

1179 Chemical Equation – PAT甲级真题

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, CH4​+2O2​=CO2​+2H2​O means that the reactants in this chemical reaction are methane and oxygen: CH4​ and O2​, and the products of this reaction are carbon dioxide and water: CO2​ and H2​O.

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 (2≤N≤20), followed by N distinct indices of reactants. The second line gives an integer M (1≤M≤10), followed by M distinct indices of products. The index of an entity is a 2-digit number.

Then a positive integer K (≤50) is given, followed by 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 { a1​,⋯,am​ } is said to be smaller than another sequence { b1​,⋯,bn​ } if there exists 1≤imin(m,n) so that aj​=bj​ for all j<i, and 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

题目大意:给定一组反应物和产物,你需要告诉我们如何获得这些产物,假设每种反应物只能使用一次。为了简单起见,我们将方程式右侧的所有实体视为一个单一的产物。打印使用给定的反应物来获得所有给定的产物的方程式。注意,每个反应物只能使用一次。

分析:used用来标记每个反应物是否可用,ans[i]表示第i个产物需要用到它的第i条合成公式(排序后),pro中存储产物的编号,equa中存储每个产物的所有合成公式。
如果某个产物自己本身就是反应物,这一条合成公式需要加到配方里。在匹配方案之前,先把每个配方按照题目要求从小到大排序。
本文搜索采用的序列是按照产物编号进行顺序搜索,确保每一个产物都在满足要求的情况下才继续下一次的搜索。
搜索过程中,尝试某产物的所有合成公式,如果当前公式中所有反应物都还可用,则进入下一层搜索(注意回来时要把这些反应物还给我们)。当达到M层时,表示已经完全匹配,并且是按照题目要求的最优序,输出每一条合成配方即可。