STEP5::Problem 0098 : 刮鬍匹配

http://web2.ck.tp.edu.tw/~step5/probdisp.php?pid=0098
這題很簡單,其實先把0放到stack裡,如果放了1進去,就看他上一個是不是0,是就一起拔掉,不是就繼續堆上去,輸出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define N 100001
using namespace std;
int ST[N],st=0;
int main(int argc,char *argv[])
{
int n,b;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&b);
if(b==1&&st>0){
if(ST[st-1]==0){st--;}
else ST[st++]=b;
}else
ST[st++]=b;
}
printf("%d\n",st);
//test
//system("pause");
return 0;
}