summaryrefslogtreecommitdiff
path: root/cpp/parentesi.cpp
blob: 159b75f143d1f7de4b198e8004a25951fd772f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* INPUT:
[ ( ( [ { { [ ] ] ( ( ( } } ( ) ) ) ) ) ) ] ] ]

output:
11 */
#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

int main()
{
	ifstream in;
	ofstream out;
	in.open("input.txt");
	out.open("output.txt");
	
	int i;
	int t = 0, q = 0, g = 0; //tonde, quadre, graffe
	char str[1000], c;
	i = 0;
	while(!in.eof())
	{
		in.get(c);
		if(c == ' ') continue;
		else {
			str[i] = c;
			i++;
		}
	}
	
	int n = strlen(str), tot = 0;
	
	for(int i = 0; i < n; i++)
	{
		if(str[i] == '(') t++;
		else if(str[i] == '[') q++;
		else if(str[i] == '{') g++;
		
		if(str[i] == ')' && t > 0) {
			t--;
			tot++;
		} else if(str[i] == ']' && q > 0) {
			q--;
			tot++;
		} else if(str[i] == '}' && g > 0) {
			g--;
			tot++;
		}
	}
	
	out << tot << endl;
	
	in.close();
	out.close();
	return 0;
}