HDU 1870

来源:互联网 发布:护眼台灯推荐 知乎 编辑:程序博客网 时间:2024/05/19 02:02

#include <iostream>#include <stack>using namespace std;char str[1010];stack<char>q;int main(){ while(cin>>str) {    int sum(0);while(!q.empty())  q.pop();   int len=strlen(str);   for(int i=0;i<len;i++)    {      if(str[i]=='(') { q.push(str[i]);sum++;}  if(str[i]==')'&&q.top()=='(')  { q.pop(); sum--; }      if(str[i]=='B') break;}      cout<<sum<<endl; }return 0;}

复习了一下栈的使用。

栈(statck)这种数据结构在计算机中是相当出名的。栈中的数据是先进后出的(First In Last Out, FILO)。栈只有一个出口,允许新增元素(只能在栈顶上增加)、移出元素(只能移出栈顶元素)、取得栈顶元素等操作。在STL中,栈是以别的容器作为底部结构,再将接口改变,使之符合栈的特性就可以了。因此实现非常的方便。下面就给出栈的函数列表和VS2008中栈的源代码,在STL中栈一共就5个常用操作函数(top()、push()、pop()、 size()、empty() ),很好记的。


转载:http://blog.csdn.net/morewindows/article/details/6950881

这题水~~




原创粉丝点击