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")