fork(2) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int solution(string str);
  8.  
  9. int main() {
  10.  
  11. cin.tie(NULL);
  12. ios::sync_with_stdio(false);
  13. string str;
  14. getline(cin, str);
  15. cout << solution(str);
  16.  
  17. return 0;
  18. }
  19.  
  20. int solution(string str)
  21. {
  22. int ret = 0;
  23. int temp = 1;
  24. string c_stc[100]; int c_top = -1;
  25.  
  26. for (int i = 0; i < str.length(); i++) {
  27. if (str[i] == '(')
  28. {
  29. c_stc[++c_top] = str[i];
  30. }
  31. if (str[i] == '[') {
  32. c_stc[++c_top] = str[i];
  33. }
  34. if (str[i] == ')' && c_top > -1) {
  35. if (c_stc[c_top] == "(")
  36. c_stc[c_top] = "2";
  37. else if (c_stc[c_top] != "(" && c_stc[c_top] != "[") {//숫자가 있을 때
  38. int top = stoi(c_stc[c_top]);
  39. int add = 0;
  40. while (c_stc[c_top - 1] != "(" && c_stc[c_top - 1] != "[") {
  41. top += stoi(c_stc[--c_top]);
  42. }
  43. if (c_stc[c_top - 1] == "(")
  44. c_stc[--c_top] = to_string(top * 2);
  45. else
  46. return 0;
  47. }
  48. }
  49. if (str[i] == ']' && c_top > -1) {
  50. if (c_stc[c_top] == "[")
  51. c_stc[c_top] = "3";
  52. else if (c_stc[c_top] != "(" && c_stc[c_top] != "[") {//숫자가 있을 때
  53. int top = stoi(c_stc[c_top]);
  54. int add = 0;
  55. while (c_stc[c_top - 1] != "(" && c_stc[c_top - 1] != "[") {
  56. top += stoi(c_stc[--c_top]);
  57. }
  58. if (c_stc[c_top - 1] == "[")
  59. c_stc[--c_top] = to_string(top * 3);
  60. else
  61. return 0;
  62. }
  63. }
  64. }
  65.  
  66.  
  67. for (int i = 0; i <= c_top; i++) {
  68. if (c_stc[i] == "[" || c_stc[i] == "(") return 0;
  69. else ret += stoi(c_stc[i]);
  70. }
  71. return ret;
  72. }
Success #stdin #stdout 0s 4256KB
stdin
][]
stdout
3