02.Brackets
Здравейте! Аз решавам задача 02.Brackets от упражнението за вектори, листове и итератори. Няколко пъти редактирах кода за тази задача. В първите ми 3 варианта някои от тестовете не ми минаваха, но специално тест 5 минаваше. В сегашния ми код, когато всчки останали минават, точно той не ме пуска. Ако може да погледнете решението ми и да ми кажете къде бъркам и какво съм счупила програмата, защото всички възможни случаи, за коио аз се сещам ги изпробвах и ми работят.
#include <iostream>
#include <vector>
#include <list>
#include <sstream>
#include <string>
#include <iterator>
using namespace std;
std::string readInput()
{
std::string expression;
std::cin>>expression;
return expression;
}
int countOpenBigBrackets(std::string expression)
{
int counter=0;
for(int i=0; i<expression.size(); i++)
{
if(expression[i]=='{') counter++;
}
return counter;
}
int countCloseBigBrackets(std::string expression)
{
int counter=0;
for(int i=0; i<expression.size(); i++)
{
if(expression[i]=='}') counter++;
}
return counter;
}
int countOpenSquareBrackets(std::string expression, int from, int to)
{
int counter=0;
for(int i=from; i<=to; i++)
{
if(expression[i]=='[') counter++;
}
return counter;
}
int countCloseSquareBrackets(std::string expression, int from, int to)
{
int counter=0;
for(int i=from; i<=to; i++)
{
if(expression[i]==']') counter++;
}
return counter;
}
int countOpenSimplyBrackets(std::string expression, int from, int to)
{
int counter=0;
for(int i=from; i<=to; i++)
{
if(expression[i]=='(') counter++;
}
return counter;
}
int countCloseSimplyBrackets(std::string expression, int from, int to)
{
int counter=0;
for(int i=from; i<=to; i++)
{
if(expression[i]==')') counter++;
}
return counter;
}
bool remakeHandleFirstIsSquareBracketLogic(std::string expression)
{
std::vector<int> index;
std::vector<int> idx;
for(int i=0; i<expression.size(); i++)
{
if(expression[i]=='['){idx.push_back(i);}
else if(expression[i]==']'){index.push_back(i);}
}
if(idx.size()!=index.size()){return false;}
for(int i=0; i<idx.size(); i++)
{
for(int m=idx[i]; m<=index[i]; m++)
{
if(expression[m]=='{'||expression[m]=='}'){return false;}
}
if( countOpenSquareBrackets(expression, idx[i], index[i])!= countCloseSquareBrackets(expression, idx[i], index[i])){return false;}
if(countOpenSimplyBrackets(expression, idx[i], index[i])!=countCloseSimplyBrackets(expression, idx[i], index[i])){return false;}
}
if( countOpenSquareBrackets(expression, 0, (expression.size()-1))!= countCloseSquareBrackets(expression, 0, (expression.size()-1))){return false;}
if(countOpenSimplyBrackets(expression, 0, (expression.size()-1))!=countCloseSimplyBrackets(expression, 0, (expression.size()-1))){return false;}
return true;
}
bool remakeHandleFirstIsSimplyBracketLogic(std::string expression)
{
//if(expression[expression.size()-1]!=')'){return false;}
std::vector<int> index;
std::vector<int> idx;
for(int i=0; i<expression.size(); i++)
{
if(expression[i]=='('){idx.push_back(i);}
if(expression[i]==')'){index.push_back(i);}
}
if(idx.size()!=index.size()){return false;}
for(int i=0; i<idx.size(); i++)
{
for(int m=idx[i]; m<=index[i]; m++)
{
if(expression[m]=='{'||expression[m]=='}'||expression[m]=='['||expression[m]==']'){return false;}
}
if(countOpenSimplyBrackets(expression, idx[i], index[i])!=countCloseSimplyBrackets(expression, idx[i], index[i])){return false;}
}
if(countOpenSimplyBrackets(expression, 0, (expression.size()-1))!=countCloseSimplyBrackets(expression, 0, (expression.size()-1))){return false;}
return true;
}
bool handleFirstIsBigBracketLogic(std::string expression)
{
//if(expression[expression.size()-1]!='}'){return false;}
if(countOpenBigBrackets(expression)!=countCloseBigBrackets(expression)){return false;}
if( countOpenSquareBrackets(expression, 0, (expression.size()-1))!= countCloseSquareBrackets(expression, 0, (expression.size()-1))){return false;}
if(countOpenSimplyBrackets(expression, 0, expression.size()-1)!=countCloseSimplyBrackets(expression, 0,( expression.size()-1))){return false;}
if(!(remakeHandleFirstIsSquareBracketLogic(expression))){return false;}
if(!(remakeHandleFirstIsSimplyBracketLogic(expression))){return false;}
return true;
}
bool handleFirstIsSquareBracketLogic(std::string expression)
{
int index=0;
for(int i=0; i<expression.size(); i++)
{
if(expression[i]==']'){index=i;break;}
if(expression[i]=='{'||expression[i]=='}'){return false;}
}
if( countOpenSquareBrackets(expression, 0, index)!= countCloseSquareBrackets(expression, 0, index)){return false;}
if(countOpenSimplyBrackets(expression, 0, index)!=countCloseSimplyBrackets(expression, 0, index)){return false;}
if(countOpenBigBrackets(expression)!=countCloseBigBrackets(expression)){return false;}
if( countOpenSquareBrackets(expression, 0, (expression.size()-1))!= countCloseSquareBrackets(expression, 0, (expression.size()-1))){return false;}
if(countOpenSimplyBrackets(expression, 0, (expression.size()-1))!=countCloseSimplyBrackets(expression, 0, (expression.size()-1))){return false;}
if(!(remakeHandleFirstIsSimplyBracketLogic(expression))){return false;}
return true;
}
bool handleFirstIsSimplyBracketLogic(std::string expression)
{
//if(expression[expression.size()-1]!=')'){return false;}
int index=0;
for(int i=0; i<expression.size(); i++)
{
if(expression[i]==')'){index=i;break;}
if(expression[i]=='{'||expression[i]=='}'||expression[i]=='['||expression[i]==']'){return false;}
}
if(countOpenSimplyBrackets(expression, 0, index)!=countCloseSimplyBrackets(expression, 0, index)){return false;}
if(countOpenBigBrackets(expression)!=countCloseBigBrackets(expression)){return false;}
if( countOpenSquareBrackets(expression, 0, (expression.size()-1))!= countCloseSquareBrackets(expression, 0, (expression.size()-1))){return false;}
if(countOpenSimplyBrackets(expression, 0, (expression.size()-1))!=countCloseSimplyBrackets(expression, 0, (expression.size()-1))){return false;}
return true;
}
void checkIsRight(std::string expression)
{
if(expression.empty()){std::cout<<"invalid"<<std::endl;}
if(expression[0]=='{')
{
if(handleFirstIsBigBracketLogic(expression) ){std::cout<<"valid"<<std::endl;}
else{std::cout<<"invalid"<<std::endl;}
}
if(expression[0]=='[')
{
if(handleFirstIsSquareBracketLogic(expression)) {std::cout<<"valid"<<std::endl;}
else {std::cout<<"invalid"<<std::endl;}
}
if(expression[0]=='(')
{
if(handleFirstIsSimplyBracketLogic(expression)) {std::cout<<"valid"<<std::endl;}
else {std::cout<<"invalid"<<std::endl;}
}
}
int main(){
std::string expression=readInput();
checkIsRight(expression);
return 0;
}
Ще съм благодарна, ако някой ме поправи и ми обясни къде бъркам!