fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <limits.h>
  4.  
  5. #define ERROR INT_MAX
  6.  
  7. int maxint(a, b) {
  8. return (a>b)?a:b;
  9. }
  10.  
  11. struct pack {
  12. char *s;
  13. int depth;
  14. };
  15.  
  16. struct pack in(char *s, int depth, int max);
  17.  
  18. struct pack out(char *s, int depth, int max) {
  19. struct pack tmp;
  20. if((*s)=='\0') {
  21. tmp.s=NULL;tmp.depth=maxint(max, depth);
  22. return tmp;
  23. }
  24. if(strchr(")]}", *s)!=NULL) {
  25. tmp.s=NULL;tmp.depth=ERROR;
  26. return tmp;
  27. }
  28. if(strchr("({[", *s)!=NULL) {
  29. tmp = in(s+1, depth+1, max);
  30. return out(tmp.s, depth, maxint(max, tmp.depth));
  31. }
  32. return out(s+1, depth, max);
  33. }
  34.  
  35. struct pack in(char *s, int depth, int max) {
  36. struct pack tmp;
  37. if((*s)=='\0') {
  38. tmp.s=s;tmp.depth=ERROR;
  39. return tmp;
  40. }
  41. if(strchr(")]}", *s)!=NULL) {
  42. tmp.s=s+1;tmp.depth=maxint(max, depth);
  43. return tmp;
  44. }
  45. if(strchr("([{", *s)!=NULL) {
  46. struct pack tmp = in(s+1, depth+1, max);
  47. return in(tmp.s, depth, maxint(max, tmp.depth));
  48. }
  49. return in(s+1, depth, max);
  50. }
  51.  
  52. int main(void) {
  53. char a[256];
  54. struct pack tmp;
  55. int i = 4;
  56. while(i--) {
  57. fgets(a, 256, stdin);
  58. tmp = out(a, 0, 0);
  59. printf("%d\n", tmp.depth);
  60. }
  61. return 0;
  62. }
  63.  
  64.  
Success #stdin #stdout 0s 2056KB
stdin
(2*3)+ {4+5 + ( 2*3*(5)+9)} + (5*7)
(2*3)+ {4+5 + ( 2*3*(5)+9)) + (5*7)
(2*3)+ {4+5 + ( 2*3*(5)+9)}) + (5*7)
(2*3)+ {4+5 + ( 2*3*(5)+9)} + (5*7

stdout
3
3
2147483647
2147483647