Pagini recente » Cod sursa (job #578611) | Cod sursa (job #1262035) | Cod sursa (job #286154) | Cod sursa (job #2757686) | Cod sursa (job #2798716)
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
ifstream fin ("paranteze.in");
ofstream fout ("paranteze.out");
bool openParanthesis(char current) {
return current == '{' || current == '[' || current == '(';
}
bool match(char opening, char ending) {
return ending - opening == 1 || ending - opening == 2;
}
int main() {
fin.tie(NULL);
std::ios_base::sync_with_stdio(false);
stack<char> parantheses;
char current;
int n, index = 0, maxParanthesized = 0;
fin >> n;
fin.get();
while (n--) {
fin.get(current);
if (openParanthesis(current)) {
parantheses.push(current);
++index;
} else if(!parantheses.empty() && !openParanthesis(current) && n){
if(match(parantheses.top(), current)) {
parantheses.pop();
++index;
maxParanthesized = max(maxParanthesized, index - (int)parantheses.size());
} else {
parantheses = stack<char>();
index = 0;
}
}
}
if (!parantheses.empty() && !openParanthesis(current) && match(parantheses.top(), current)) {
++index;
parantheses.pop();
if (parantheses.empty()) {
maxParanthesized = max(maxParanthesized, index - (int)parantheses.size());
}
}
fout << maxParanthesized;
}