Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stack> //1 вариант
- #include <string>
- #include <fstream>
- using namespace std;
- int main()
- {
- ifstream in("input.txt");
- ofstream out("output.txt");
- stack <char> b;
- string brackets = "()[]{}";
- string tmp;
- getline(in,tmp);
- in.close();
- char a;
- int k = 0;
- for(int i = 0; i < (int)tmp.length(); i++)
- {
- a = tmp[i];
- if (brackets.find(a) % 2 == 0)
- b.push(a);
- else if ( b.empty())
- {
- out << "Not" << endl;
- k++;
- break;
- }
- else if ( b.top() != brackets [brackets.find(a) - 1])
- {
- out << "Not" << endl;
- k++;
- break;
- }
- else b.pop();
- }
- if(k == 0)
- if(b.empty())
- out << "Yes" << endl;
- out.close();
- return 0;
- }
- // 2 вариант
- #include <stack>
- #include <string>
- #include <fstream>
- using namespace std;
- int main()
- {
- ifstream in("input.txt");
- ofstream out("output.txt");
- stack <char> b;
- string brackets = "()[]{}";
- char a;
- int k =0;
- while(in.peek() != EOF)
- {
- in >> a;
- if (brackets.find(a) % 2 == 0)
- b.push(a);
- else if ( b.empty())
- {
- out << "Not" << endl;
- k++;
- break;
- }
- else if ( b.top() != brackets [brackets.find(a) - 1])
- {
- out << "Not" << endl;
- k++;
- break;
- }
- else b.pop();
- }
- if(k == 0)
- if(b.empty())
- out << "Yes" << endl;
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement