[关闭]
@lychee123 2017-03-18T11:52:06.000000Z 字数 756 阅读 1117

UVA-673(栈的简单使用,判断括号是否匹配)

STL


  1. #include<stdio.h>
  2. #include<stack>
  3. #include<string>
  4. #include<string.h>
  5. #include<algorithm>
  6. using namespace std;
  7. int main()
  8. {
  9. int n,i;
  10. char a[222];
  11. scanf("%d",&n);
  12. getchar();///吸收输入n的换行符
  13. while(n--)
  14. {
  15. gets(a);///可以输入空格
  16. if(a[0]==' ')
  17. printf("Yes\n");
  18. else
  19. {
  20. stack<char>st;
  21. int l=strlen(a);
  22. for(i=0;i<l;i++)
  23. {
  24. if(st.empty())
  25. st.push(a[i]);
  26. else if(a[i]=='('||a[i]=='[')
  27. st.push(a[i]);
  28. else if(a[i]==')'&&st.top()=='(')
  29. st.pop();
  30. else if(a[i]==']'&&st.top()=='[')
  31. st.pop();
  32. else
  33. st.push(a[i]);
  34. }
  35. if(!st.empty())
  36. printf("No\n");
  37. else
  38. printf("Yes\n");
  39. }
  40. }
  41. return 0;
  42. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注