[specific]bracketを含むパーシング
date: 2021-03-28 excerpt: [specific]bracketを含むパーシング
bracketを含むパーシング
- データ構造
stack
の適応例
問題の定義
具体的なソリューション
def find_mismatch(text):
opening_brackets_stack = []
for i, elm in enumerate(text):
if elm in "([{":
opening_brackets_stack.append([elm, i])
if elm in ")]}":
if elm == ")":
elm = "("
elif elm == "]":
elm = "["
else:
elm = "{"
try:
if opening_brackets_stack.pop()[0] != elm:
return i + 1
except:
return i + 1
if len(opening_brackets_stack) == 0:
return "Success"
else:
return opening_brackets_stack[0][1] + 1
def main():
text = input()
r = find_mismatch(text)
print(r)
if __name__ == "__main__":
main()