Cod sursa(job #2625213)

Utilizator ihorvaldsTudor Croitoru ihorvalds Data 5 iunie 2020 20:04:44
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <algorithm>

bool matches(char& s, char& c) {
	if (s == '(' && c == ')')
		return true;

	if (s == '[' && c == ']')
		return true;

	if (s == '{' && c == '}')
		return true;

	return false;
}

int main()
{
	std::ifstream f("paranteze.in");
	std::ofstream g("paranteze.out");

	int m = 0, n_m = 0;
	int tmp;
	std::string s;
	std::stack<int> st;

	f >> tmp;
	f >> s;
	st.push(0);

	for (int i = 1; i <= s.size(); i++) {
		if (!st.empty() && matches(s[st.top()], s[i])) {
			st.pop();
			if (!st.empty())
				n_m = i - st.top();
			else
				n_m = i;
			if (n_m > m) m = n_m;
		} else {
			st.push(i);
		}
	}

	g << m << "\n";
}