输入输出样例

样例1

输入

  1. 11
  2. H2+O2=H2O
  3. 2H2+O2=2H2O
  4. H2+Cl2=2NaCl
  5. H2+Cl2=2HCl
  6. CH4+2O2=CO2+2H2O
  7. CaCl2+2AgNO3=Ca(NO3)2+2AgCl
  8. 3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
  9. 3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
  10. 4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
  11. 4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
  12. Cu+As=Cs+Au

输出

  1. N
  2. Y
  3. N
  4. Y
  5. Y
  6. Y
  7. Y
  8. Y
  9. Y
  10. Y
  11. N

题解一

用Map来存储化学元素及其次数。难点在于嵌套括号的处理。100分。
参考:https://blog.csdn.net/qq_18287147/article/details/106981698?utm_medium=distribute.pc_relevant_download.none-task-blog-blogcommendfrombaidu-1.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-blogcommendfrombaidu-1.nonecas

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class Main {
  7. public static void main(String[] args) throws IOException {
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  9. int n = Integer.parseInt(reader.readLine());
  10. for (int i = 0; i < n; ++i) {
  11. String raw = reader.readLine();
  12. String[] strs = raw.split("=");
  13. String left = strs[0];
  14. String right = strs[1];
  15. Map<String, Integer> mapA = handle(left);
  16. Map<String, Integer> mapB = handle(right);
  17. // System.out.println(mapA);
  18. // System.out.println(mapB);
  19. // System.out.println(mapA.equals(mapB));
  20. if (mapA.equals(mapB)){
  21. System.out.println("Y");
  22. }else {
  23. System.out.println("N");
  24. }
  25. }
  26. }
  27. private static Map<String, Integer> handle(String s) {
  28. Map<String, Integer> ans = new HashMap<>();
  29. for (String str : s.split("\\+")) {
  30. // System.out.println(str);
  31. int coef = 0;
  32. int i;
  33. for (i = 0; i < str.length(); ++i) {
  34. char ch = str.charAt(i);
  35. if (isDigit(ch)) {
  36. coef = coef * 10 + ch - 48;
  37. } else {
  38. break;
  39. }
  40. }
  41. handlePart(ans, str, i, str.length() - 1, Math.max(1, coef));
  42. }
  43. return ans;
  44. }
  45. private static void handlePart(Map<String, Integer> map, String s, int left, int right, int coef) {
  46. int i = left;
  47. while (i <= right) {
  48. if (s.charAt(i) == '(') { // 找到与这个左括号对应的右括号, 递归解析
  49. int depth = 1;
  50. int index;
  51. for (index = i + 1, depth = 1; depth > 0; ++index) {
  52. if (s.charAt(index) == '(') {
  53. ++depth;
  54. } else if (s.charAt(index) == ')') {
  55. --depth;
  56. }
  57. }
  58. // 右括号左侧一位
  59. int r = index - 2;
  60. // 角标倍数
  61. int x = 0;
  62. while ((index <= right) && (isDigit(s.charAt(index)))) {
  63. x = x * 10 + s.charAt(index) - 48;
  64. ++index;
  65. }
  66. handlePart(map, s, i + 1, r, Math.max(coef, coef * x));
  67. i = index;
  68. } else if (s.charAt(i) == ')') {
  69. ++i;
  70. } else { // 化学元素首大写字母
  71. StringBuilder sBuilder = new StringBuilder();
  72. sBuilder.append(s.charAt(i));
  73. ++i;
  74. while ((i <= right) && (isLowerCase(s.charAt(i)))) {
  75. sBuilder.append(s.charAt(i));
  76. ++i;
  77. }
  78. // 元素符号
  79. String element = sBuilder.toString();
  80. // 角标倍数
  81. int x = 0;
  82. while ((i <= right) && (isDigit(s.charAt(i)))) {
  83. x = x * 10 + s.charAt(i) - 48;
  84. ++i;
  85. }
  86. map.put(element, (int) map.getOrDefault(element, 0) + Math.max(coef, x * coef));
  87. }
  88. }
  89. }
  90. private static boolean isDigit(char ch) {
  91. return (ch >= '0') && (ch <= '9');
  92. }
  93. private static boolean isLowerCase(char ch) {
  94. return (ch >= 'a') && (ch <= 'z');
  95. }
  96. }