fork download
  1. //#include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. /*int check(char d[], char result[],int i,int len) {
  8. while (d[i] != '(' && d[i] != ')' && i<len) {
  9. //cout << "test1 i = " << i << endl;
  10. i++;
  11. }
  12. //cout << "test2 i = " << i << endl;
  13. if (i < len) {
  14. //cout << "test3 i = " << i << endl;
  15. if (d[i] == '(')
  16. {
  17. //cout << "test4 i = " << i << endl;
  18. num++;
  19. int w = check(d, result, i + 1, len);
  20. //cout << "test6 i = " << w << endl;
  21. if (w == 0) {
  22. cout << i << endl;
  23. result[i] = '?';
  24. return false;
  25. }
  26. else {
  27. return check(d, result, i + 1, len);
  28. }
  29. }
  30. else if (d[i] = ')') {
  31. cout << "test5 i = " << i << " " << num << endl;
  32. if (num > 0) {
  33. num--;
  34. check(d, result, i+1,len);
  35. return 1;
  36. }
  37. else {
  38. cout << "sss" << i << endl;
  39. result[i] = '$';
  40. check(d, result, i + 1, len);
  41. }
  42. }
  43. }
  44. else {
  45. if (num > 0) {
  46. return 0;
  47. }
  48. else {
  49. return 1;
  50. }
  51. }
  52. }*/
  53. int num = 0;
  54.  
  55. int check(char d[], char result[], int i, int len) {
  56. while (d[i] != '(' && d[i] != ')' && i<len) {
  57. i++;
  58. }// if the character is not '()', skip
  59.  
  60. if (i < len) {
  61. if(d[i] == '('){
  62. num++;
  63. int loc = check(d, result, i + 1, len);
  64. // return value loc should be the location of corresponding ')'
  65. if (loc == -1) // means hitting the end of the str
  66. {
  67. result[i] = '&';
  68. return -1;
  69. }
  70. else {
  71. return check(d, result, loc + 1, len); // keep retrieving after ')'
  72. }
  73. }
  74. else if (d[i] == ')' && num <= 0) {
  75. // if num == 0 and get ')', means ')' doesn't have a match
  76. while (d[i] == ')' && i < len) {
  77. //cout << "test1 " << num << endl;
  78. result[i] = '?';
  79. i++;
  80. }
  81.  
  82. check(d, result, i, len); //keep going
  83. return i;
  84. }
  85. else { // return the location of the ')' and subtract num
  86. if (num > 0) num--;
  87. return i;
  88. }
  89. }
  90.  
  91. else { // hitting the end, return -1
  92. return -1;
  93. }
  94. }
  95. int main() {
  96. char d[100];
  97. while (cin.getline(d, 101)) {
  98. int len = strlen(d);
  99. char result[100] = { ' ' };
  100. for (int i = 0; i < len; i++) result[i] = ' ';
  101. for (int i = 0; i < len; i++) cout << d[i];
  102. cout << endl;
  103. num = 0;
  104. check(d, result, 0, len);
  105. for (int i = 0; i < len; i++) cout << result[i];
  106. cout << endl;
  107. }
  108. return 0;
  109. }
Success #stdin #stdout 0s 15232KB
stdin
((ABCD(x)
)(rttyy())sss)(()((())))))<<((((
stdout
((ABCD(x)
&&       
)(rttyy())sss)(()((())))))<<((((
?            ?          ??  &&&&