Advertisement
Mikhail-Podbolotov

Untitled

May 8th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. string findresult(std::string& input) {
  8.     string result = "";
  9.     int maxDepth = -1;
  10.     int currentDepth = 0;
  11.     bool flag = false;
  12.     for (char c : input) {
  13.         if (c == '(' || c == '{' || c == '[') {
  14.             currentDepth++;
  15.             if (currentDepth > maxDepth) {
  16.                 flag = false;
  17.                 maxDepth = currentDepth;
  18.                 result = "";
  19.             }
  20.         }
  21.         else if (c == ')' || c == '}' || c == ']') {
  22.             if (currentDepth == maxDepth) {
  23.                 flag = true;
  24.             }
  25.             currentDepth = 0;
  26.            
  27.         }
  28.         else if (currentDepth == maxDepth and !flag) {
  29.             result += c;
  30.         }
  31.         cout << result << endl;
  32.     }
  33.  
  34.     return result;
  35. }
  36. int main()
  37. {
  38.     ifstream input("input.txt");
  39.     ofstream output("output.txt");
  40.     string read = "";
  41.     string cur = "";
  42.     while (!input.eof()) {
  43.         getline(input, cur);
  44.         read += cur;
  45.         cur = "";
  46.     }
  47.     string result = findresult(read);
  48.     output << result;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement