CCF CSP2019.12 -化学方程式 python

这篇博客主要介绍了如何使用栈数据结构处理复杂的化学方程式,包括括号的嵌套处理。作者分享了详细的代码实现,通过读取输入的化学方程式,将分子拆分成元素并计算数量,然后对比左右两侧的元素是否相同来判断方程式是否平衡。博客内容涉及到字符串处理、数据结构应用以及算法设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CCF CSP2019.12 – 化学方程式

本题我做的有点复杂,不过我可以给大家分享一下我的思路,第三题字符串一般都是很复杂情况,比较多,就暴力判断,先写出一个大致框架,后期再根据各种特殊情况来修改代码。
本题在处理括号的时候我用了栈的数据结构来处理,具体思路如下:在遇到左括号时将接下来的元素进栈直到遇见右括号为之;遇见右括号就往前处理找到之前对应的左括号处理,处理的结果是将这个括号去掉,依次这样处理来处理括号嵌套的情况 。
最后贴上我的代码:写的比较啰嗦。

n=int(input())
def fun1(k):
  # :param k: 化学方程式的每一个项
  # :return: 元素列表以及其对应的出现次数
  h = 0
  xiNum = 1
  if k[0].isdigit():
      xiNum = int(k[0])
      h += 1
      for z in range(1, len(k)):
          if k[z].isdigit():
              h += 1
              xiNum = xiNum * 10 + int(k[z])
          else:
              break
  stack0 = []
  num = []
  tempNum = []
  stack1 = []
  stack2 = []
  stack3 = []
  while h < len(k):
      # if h+1==len(k):
      #     stack0.append(k[h])
      #     num.append(1)
      #     h+=1
      if k[h].isupper() and k[h + 1].islower():
          stack0.append(k[h] + k[h + 1])
          if h+2==len(k):
              num.append(1)
              h+=2
          else:
              if k[h + 2].isdigit():
                  tmp=int(k[h+2])
                  h+=1
                  if h+2 <len(k):
                      while k[h + 2].isdigit():
                          tmp=int(k[h+2])+tmp*10
                          h+=1
                          if h+2 >= len(k):
                              break
                      num.append(tmp)
                      h += 2
                  else:
                      num.append(tmp)
                      h+=2
              else:
                  h += 2
                  num.append(1)
      elif k[h].isupper() and not k[h + 1].islower():
          stack0.append(k[h])
          if k[h + 1].isdigit():
              tmp = int(k[h + 1])
              h += 1
              if h+1 <len(k):
                  while k[h + 1].isdigit():
                      tmp = int(k[h + 1]) + tmp * 10
                      h += 1
                      if h + 1 >= len(k):
                          break
                  num.append(tmp)
                  h += 1
              else:
                num.append(tmp)
                h+=1
          else:
              num.append(1)
              h += 1
      elif k[h] == "(":  # h=3
          stack1.append("(")
          h += 1
          while k[h] != ")":
              if k[h].isupper() and k[h + 1].islower():
                  stack1.append(k[h] + k[h + 1])
                  if k[h + 2].isdigit():
                      tmp = int(k[h + 2])
                      h += 1
                      if h+1 < len(k):
                          while k[h + 2].isdigit() and h+2<len(k):
                              tmp = int(k[h + 2]) + tmp * 10
                              h += 1
                              if h+2 >= len(k):
                                  break
                          tempNum.append(tmp)
                          h += 2
                      else:
                          tempNum.append(tmp)
                          h+=2
                  else:
                      tempNum.append(1)
                      h += 2
              elif k[h].isupper() and not k[h + 1].islower():
                  stack1.append(k[h])
                  if k[h + 1].isdigit():
                      tmp = int(k[h + 1])
                      h += 1
                      if h+1 <len(k):
                          while k[h + 1].isdigit() and h+1<len(k):
                              tmp = int(k[h + 1]) + tmp * 10
                              h += 1
                              if h+1 >= len(k):
                                  break
                          tempNum.append(tmp)
                          h += 1
                      else:
                          tempNum.append(tmp)
                          h+=1
                  else:
                      tempNum.append(1)
                      h += 1
              else:
                  stack1.append(k[h])
                  h += 1
      elif k[h] == ")" and stack1.count("(") != 0:
          a = stack1.pop()
          stack3.append(a)
          if h+1 == len(k):
              numt0=1
              h+=1
          elif k[h + 1].isdigit():
              tmp = int(k[h + 1])
              h += 1
              if h+1 < len(k):
                  while k[h + 1].isdigit():
                      tmp = int(k[h + 1]) + tmp * 10
                      h += 1
                      if h + 1 >= len(k):
                          break
                  numt0 = tmp
                  h+=1
              else:
                  numt0=tmp
                  h+=1
          else:
              numt0=1
              h += 1
          while a != "(":
              numt = tempNum.pop() * numt0
              stack2.append(numt)
              a = stack1.pop()
              stack3.append(a)
          stack3.pop()
          for i in stack2[::-1]:
              tempNum.append(i)
          for j in stack3[::-1]:
              stack1.append(j)
          stack3 = []
          stack2 = []
          if stack1.count("(") == 0:
              stack0 = stack0 + stack1
              num = num + tempNum
  return stack0, [i * xiNum for i in num]
for i in range(n):
  left = []
  right = []
  dictLeft = {}
  dictRight = {}
  x=input()
  left.append(x.split("=")[0])
  right.append(x.split("=")[1])
  for j in left:
      t0 = j.split("+")  # 把每一个项分出来对每个项来进行处理
      for k in t0:  # k是每一个项
          if  not k[-1].isdigit():
              k+="1"
          v, n = fun1(k)
          for i in range(len(v)):
              if v[i] not in dictLeft:
                  dictLeft.update({v[i]: n[i]})
              else:
                  dictLeft[v[i]] += n[i]
  for j in right:
      t0 = j.split("+")  # 把每一个项分出来对每个项来进行处理
      for k in t0:  # k是每一个项
          if not k[-1].isdigit():
              k += "1"
          v, n = fun1(k)
          for i in range(len(v)):
              if v[i] not in dictRight:
                  dictRight.update({v[i]: n[i]})
              else:
                  dictRight[v[i]] += n[i]
  dictL = sorted(dictLeft.items(), key=lambda d: d[0])
  dictR = sorted(dictRight.items(), key=lambda d: d[0])
  if dictL == dictR:
      print("Y")
  else:
      print("N")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值