题目链接
后最表达式的值
s = input().strip().split()
st = list(map(int, s[:-1]))
op = s[-1][:-1]
for t in op:
a ,b = st.pop(),st.pop()
st.append(eval(str(b)+t+str(a)))
print(st[0])
表达式括号匹配(stack)
import re
s = input()
res = re.findall(r'(\(|\))',s)
st = []
for c in res:
if st and c==')':
if st.pop() != '(':
print('NO')
break
else:
st.append(c)
else:
if st:
print('NO')
else:
print('YES')
括弧匹配检验
s = input().strip()
stk = []
for c in s:
if stk and c in [')',']']:
if stk[-1] == '(' and c == ')':
stk.pop()
elif stk[-1] == '[' and c == ']':
stk.pop()
else:
stk.append(c)
if stk:
print('Wrong')
else:
print('OK')
字符串匹配问题
n = int(input())
left = ['<','(','[','{']
right = ['>',')',']','}']
now_left = []
for _ in range(n):
s = input().strip()
st =[]
flag = 0
for c in s:
if st and c in right:
if left.index(st[-1]) == right.index(c):
st.pop()
else:
flag = 1
break
elif not st and c in right:
flag = 1
break
else:
if st and left.index(c) > left.index(st[-1]):
flag = 1
break
else:
st.append(c)
if flag :
print('NO')
else:
if st:
print('NO')
else:
print('YES')
# 计算
python独特方法
```python
s = input().replace('^','**').replace('/','//')
print(eval(s))
常规写法
def level(c): # 判断符号的优先级
if c == '+' or c == '-':
return 1
elif c == '*' or c == '/':
return 2
elif c == '^':
return 3
return 0
def calculation(): # 将digit栈中最顶部两个数取出,进行计算
a = digit.pop()
b = digit.pop()
ch = symbol.pop()
if ch == '+':
digit.append(a + b)
elif ch == '-':
digit.append(b - a)
elif ch == '*':
digit.append(a * b)
elif ch == '/':
digit.append(b // a)
elif ch == '^':
digit.append(pow(b, a))
digit = [] # 数字栈
symbol = [] # 符号栈
s = "(" + input() + ")"
num = ""
for c in s:
#print(c, digit, symbol)
if c.isdigit():
num += c
else: # 非数字
if num:
digit.append(int(num))
num = ""
if c == '(': # 左括号就入栈symbol
symbol.append(c)
elif c == ')': # 右括号就把左右括号之间的符号进行运算
while symbol[-1] != '(':
calculation()
symbol.pop()
else:
while symbol and level(symbol[-1]) >= level(c): # 判断优先级
calculation() # 此时符号优先级低于symbol栈顶元素的话 便进行计算
symbol.append(c) # 优先级高的符号入栈
print(digit[0]) # digit栈中只有一个元素 即为表达式的结果
车厢调度
n = int(input())
a = list(map(int,input().split()))
stk = []
i,b = 0,1
while i<n and b <= n+1:
if stk and a[i] == stk[-1]:
i += 1
stk.pop()
else:
stk.append(b)
b+=1
if i>n-1:
print('YES')
else:
print('NO')
中缀表达式值
python特有解法
s = input()[:-1]
try:
res = eval(s)
print(res)
except:
print("NO")
常规解法
def level(c): # 判断符号的优先级
if c == '+' or c == '-':
return 1
elif c == '*' or c == '/':
return 2
elif c == '^':
return 3
return 0
def calculation(): # 将digit栈中最顶部两个数取出,进行计算
a = digit.pop()
b = digit.pop()
ch = symbol.pop()
if ch == '+':
digit.append(a + b)
elif ch == '-':
digit.append(b - a)
elif ch == '*':
digit.append(a * b)
elif ch == '/':
digit.append(b // a)
elif ch == '^':
digit.append(pow(b, a))
# 判断s是否符合
def judge(s):
top = 0
if s[0] in ['+','-','*','/'] or s[-1] in ['+','-','*','/']:
return 0
for i in range(len(s)-1):
if s[i] == '(':
top += 1
elif s[i] == ')' or (i==len(s)-2 and s[i+1]==')'):
if top < 0:
return 0
top -= 1
elif s[i] in ['+','-','*','/'] and s[i+1] in ['+','-','*','/']:
return 0
if top:
return 0
return 1
digit = [] # 数字栈
symbol = [] # 符号栈
s = input()[:-1]
num = ""
if not judge(s):
print('NO')
else:
s = "(" + s + ")"
for c in s:
#print(c, digit, symbol)
if c.isdigit():
num += c
else: # 非数字
if num:
digit.append(int(num))
num = ""
if c == '(': # 左括号就入栈symbol
symbol.append(c)
elif c == ')': # 右括号就把左右括号之间的符号进行运算
while symbol[-1] != '(':
calculation()
symbol.pop()
else:
while symbol and level(symbol[-1]) >= level(c): # 判断优先级
calculation() # 此时符号优先级低于symbol栈顶元素的话 便进行计算
symbol.append(c) # 优先级高的符号入栈
print(digit[0]) # digit栈中只有一个元素 即为表达式的结果