这里是栈的做法
递归快写吐了,决心用栈,结果调了半天。。。
应该都能想到,FD就加,BF就减,REPEAT就压栈,']'就弹栈
手写栈比较舒服,用STL的stack有点丑
细节太重要了
敲的时候应该注意:读入是否从有效字符开始(用char[]的注意)、']'的读入、用过的那层栈要清空。。。
都是废话,自己写写就知道了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include <cstdio> #include <cctype> #include <string> #include <iostream> #include <cmath> using namespace std ;namespace BANANA {inline int read () { int re = 0 , k = 1 ; char in = getchar(); while (!isdigit (in)) { if (in == '-' ) k = -1 ; in = getchar(); } while (isdigit (in)) re = re * 10 + in - '0' , in = getchar(); return re * k; } int stack [305 ], k[305 ];string s;int num (string s) { int ans = 0 ; for (int i = 0 ; isdigit (s[i]); i++) ans = ans * 10 + s[i] - '0' ; return ans; } inline void main () { int top = 1 ; while (cin >> s) { if (s[0 ] == 'F' ) { cin >> s; stack [top] += num(s); int len = s.size() - 1 ; while (s[len] == ']' ) stack [top-1 ] += k[top-1 ] * stack [top], top--, len--; } else if (s[0 ] == 'B' ) { cin >> s; stack [top] -= num(s); int len = s.size() - 1 ; while (s[len] == ']' ) stack [top-1 ] += k[top-1 ] * stack [top], top--, len--; } else if (s[0 ] == 'R' ) { k[top] = read(); top++; stack [top] = 0 ; } else { int i = 0 ; while (s[i] == ']' && i < s.size()) stack [top-1 ] += k[top-1 ] * stack [top], top--, i++; } } printf ("%d\n" , abs (stack [1 ])); } } int main () { BANANA::main(); return 0 ; }