((((B)()))()) (B)
4 1
package stack;
import java.util.Scanner;
public class P1870 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String s=sc.nextLine();
Stack stack=new Stack(s.length());
char[] ch=s.toCharArray();
for(int i=0;i<ch.length;i++){
if(ch[i]=='('){
stack.inStack(ch[i]);
}else if(ch[i]==')'){
stack.outStack();
}else{
break;
}
}
System.out.println(stack.getTop());
}
}
}
class Stack{
int top=0;
int maxLen;
char[] stack;
public Stack(int maxLen){
this.maxLen=maxLen;
stack=new char[this.maxLen];
}
public void inStack(char ch){
stack[top++]=ch;
}
public void outStack(){
top--;
}
public int getTop(){
return top;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u011479875/article/details/47382853