括号配对问题——请选择合适的数据结构

2013-09-13 22:21:26

转自:http://blog.chinaunix.net/uid-24118190-id-3896712.html

C++的STL提供了一些封闭好的数据结构和算法,方便日常编程。
所谓 “工欲善其事,必先利其器。”请在学习了STL后,选择一种合适的工具来解决括号配对问题。

Containers library - cppreference.com
http://en.cppreference.com/w/cpp/container

这里使用stack解答了一个括号配对问题,但这个代码没有使用“最合适”的工具。
请选择一种合适的数据结构实现本题“扩展”要求:

点击(此处)折叠或打开

  1. /*
  2. 括号配对
  3. 配对原则: 3种括号()[]{}它们可以彼此包含,但不能交叉。
  4. 请实现 isValidSeq,测试给定字符串中括号是否正确配对。若括号匹配返回 true(1)
  5. ref: http://blog.csdn.net/bxyill/article/details/8040983
  6. pz
  7. 2013-9-11
  8. 扩展(作业):
  9. {} 只允许被 {} 包含。
  10. 如:
  11. {([])} OK
  12. {{}[]} OK
  13. ({}) NG
  14. tip:
  15. 实现基本要求使用stack即可。
  16. 实现扩展要求需要一种能够遍历的stack,若当前字符是{则遍历栈中是否有其它括号。
  17. 即使用数组实现stack能够满足要求。
  18. */
  19. #include <iostream>
  20. #include <stack>
  21. #include <assert.h>
  22. using namespace std;
  23. bool isValidSeq(char *s);
  24. int main()
  25. {
  26.     char *testdata[] = {// base,extend
  27.         "()[]{}",        // OK,OK
  28.         "(([])){}",        // OK,OK 多重括号
  29.         "{(){}[]}",        // OK,NG 嵌套:{}包含其它
  30.         "(){{}[}]",        // OK,NG 交叉:[}
  31.         "(+){{*}[ ]} "    // NG 夹杂非括号
  32.         };
  33.         
  34.     for(int i=0; i<sizeof(testdata)/sizeof(testdata[0]); i++)
  35.     {
  36.         cout << i << " TEST: " << testdata[i] << endl;
  37.         bool res = isValidSeq(testdata[i]);
  38.         cout <<"Test result: "<< (char*)(res?"OK":"NG") << endl<< endl;
  39.     }
  40.     
  41.     
  42. return 0;
  43. }
  44. // return:
  45. // {[{ -1-2-3
  46. // )]} +1+2+3
  47. // else 0
  48. int BracketType(char c)
  49. {
  50.     int res = 0;
  51.     if(c == '(')
  52.         res = -1;
  53.     else if(c == ')')
  54.         res = 1;
  55.     else if(c == '[')
  56.         res = -2;
  57.     else if(c == ']')
  58.         res = 2;
  59.     else if(c == '{')
  60.         res = -3;
  61.     else if(c == '}')
  62.         res = 3;
  63.     else 
  64.         res = 0;
  65.     return res;
  66. }
  67. // 简单匹配
  68. bool isValidSeq(char *s)
  69. {
  70.     assert(s != NULL);
  71.     stack<int> st;
  72.     int match = 0; // -1 err; 0 ign; 1 match
  73.     for(char *p = s; *p != '\0'; p++)
  74.     {
  75.         int bt = BracketType(*p);
  76.         if(bt == 0)
  77.             match = 0;
  78.         else if(bt < 0)
  79.         {
  80.             match = 0;
  81.             st.push(bt);
  82.         }
  83.         else
  84.         {
  85.             if(st.empty())
  86.                 match = -1;
  87.             if(st.top() == -bt)
  88.             {
  89.                 match = 1;
  90.                 st.pop();
  91.             }
  92.             else
  93.                 match = -1;
  94.         }
  95.         
  96.         cout<< *p ;
  97.         if(match == -1)
  98.         {
  99.             cout<<" mismatch" <<endl;
  100.             return 0;
  101.         }
  102.         else if(match == 1)
  103.             cout<<" match" <<endl;
  104.     }
  105.     
  106.     //cout<<endl;
  107.     
  108.     return st.empty();
  109. }
posted @ 2013-09-13 22:22  永不止步,永无止境  阅读(373)  评论(0)    收藏  举报