codeforces 612 C. Replace To Make Regular Bracket Sequence

http://codeforces.com/contest/612/problem/C 题意:其实就是栈的基本操作。。水题。

/* ***********************************************
Author :111qqz
Created Time :2015年12月25日 星期五 22时58分50秒
File Name :code/cf/edu4/C.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E6+7;
7int len;
8char st[N];
9int cost = 0 ;
char a[N];
int n ;
 1int which(char ch)
 2{
 3    if (ch=='<'||ch=='{'||ch=='('||ch=='[') return 1;
 4    return 2;
 5}
 6int kin(char ch)
 7{
 8    if (ch=='{'||ch=='}') return 1;
 9    if (ch=='['||ch==']') return 2;
10    if (ch=='<'||ch=='>') return 3;
11    if (ch=='('||ch==')') return 4;
12}
13bool ok(char x,char y)
14{
15    int res = 0 ;
16    if(x=='<'||x=='{'||x=='['||x=='(') res++;
17    if (y=='>'||y=='}'||y==']'||y==')') res++;
18    if (res==2)
19    {
20	if (kin(x)!=kin(y)) cost++;
21//	cout<<"x:"<<x<<" y:"<<y<<endl;
22	return true;
23    }
24    return false;
25}
1int main()
2{
3	#ifndef  ONLINE_JUDGE 
4	freopen("code/in.txt","r",stdin);
5  #endif
6	cin>>st;
7	len = strlen(st);
8	int head = -1;
9	int ans = 0 ;
 1	for ( int i = 0 ; i < len ; i++)
 2	{
 3	    if (head==-1)
 4	    {
 5		head++;
 6		a[head] = st[i];
 7		if (which(st[i])==2)
 8		{
 9		    puts("Impossible");
10		    return 0;
11		}
 1		continue;
 2	    }
 3		if (ok(a[head],st[i]))
 4		{
 5		    head--;
 6		}
 7		else
 8		{
 9		    head++;
10		    a[head] = st[i];
11		}
 1//	    cout<<"head:"<<head<<endl;
 2	}
 3//	cout<<"head:"<<head<<endl;
 4	if (head!=-1)
 5	{
 6	    puts("Impossible");
 7	}
 8	else
 9	{
10	    cout<<cost<<endl;
11	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}