Pagini recente » Cod sursa (job #2816094) | Cod sursa (job #2971146) | Cod sursa (job #2402057) | Cod sursa (job #2863906) | Cod sursa (job #2783843)
#include <fstream>
#include <string>
#include <stack>
using namespace std;
bool verif(string s)
{
stack <char> stiva;
for(auto ch: s)
{
if(ch == ')')
{
if(stiva.empty() or stiva.top() != '(')
{
return false;
}
stiva.pop();
}
else if(ch == '}')
{
if(stiva.empty() or stiva.top() != '{')
{
return false;
}
stiva.pop();
}
else
{
stiva.push(ch);
}
}
return stiva.empty();
}
int main()
{
ifstream cin("paranteze1.in");
ofstream cout("paranteze1.out");
string s;
cin >> s;
if(!verif(s))
{
cout << "-1";
}
else
{
stack <char> stiva;
for(auto ch: s)
{
if(ch == ')')
{
if(stiva.empty() or stiva.top() != '(')
{
stiva.push(ch);
}
stiva.pop();
}
else if(ch == '}')
{
if(stiva.empty() or stiva.top() != '{')
{
stiva.push(ch);
}
}
}
cout << stiva.size();
}
return 0;
}