原题链接:http://codeforces.com/contest/612/problem/C
字符匹配问题,用栈模拟一下。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <map>
#include <string>
using namespace std;
stack<char> s;
string str;
map<char, int> mp1, mp2;
int main()
{
int i, j;
int cnt = 0;
int flag = 0;
mp1['['] = mp2[']'] = 0;
mp1['('] = mp2[')'] = 1;
mp1['{'] = mp2['}'] = 2;
mp1['<'] = mp2['>'] = 3;
cin >> str;
for (i = 0; i < str.size(); i++)
{
if (str[i] == '[' || str[i] == '(' || str[i] == '{' || str[i] == '<')
{
s.push(str[i]);
}
else
{
if (s.empty())
{
flag = 1;
break;
}
else
{
if (mp1[s.top()] != mp2[str[i]])
cnt++;
s.pop();
}
}
}
if (!s.empty())//出来之后栈里面还有字符就是没匹配完
flag = 1;
if (flag)
printf("Impossible\n");
else
printf("%d\n", cnt);
return 0;
}