Pagini recente » Cod sursa (job #1456641) | Cod sursa (job #565171) | Cod sursa (job #2708492) | Cod sursa (job #2623164) | Cod sursa (job #2625213)
#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";
}