Advertisement
HabKaffee

Untitled

Nov 8th, 2020
146
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <typeinfo>
  6. #include <string>
  7.  
  8. std::ifstream FileIn ("/home/habkaffee/Cpp/Brackets/test.txt");
  9.  
  10. long long checkBrackets (std::string str) {
  11.  
  12.     if (!str.compare("")){
  13.         return 1;
  14.     }
  15.  
  16.     std::string str1 = str;
  17.     unsigned n = str1.size();
  18.     str1 = ' ' + str1;
  19.     std::vector<std::vector<unsigned>> array(1+n, std::vector<unsigned>(1+n,0));
  20.     array[0][0] = 1;
  21.  
  22.     //i - length of bracket sequence; j - how much brackets open and no closed
  23.  
  24.     for (size_t i = 1; i<=n; ++i){
  25.         for (size_t j = 0; j<=n;++j){
  26.             if ( j-1>=0 && (str1[i] == '?' || str1[i] == '(') ){
  27.                 array[i][j] += array[i-1][j-1];
  28.             }
  29.             if ( (j+1<=n/2) && (str1[i] == '?' || str1[i] == ')') ){
  30.                 array[i][j] += array[i-1][j+1];
  31.             }
  32.         }
  33.     }
  34.  
  35.  
  36.     return array[n][0];
  37. }
  38.  
  39.  
  40. int main(){
  41.  
  42.     if (!FileIn.is_open()){
  43.         std::cout<<"File isn't open";
  44.         return -1;
  45.     }
  46.  
  47.     std::string str = "";
  48.     FileIn>>str;
  49.     std::cout << str<<std::endl;
  50.     std::cout<< checkBrackets(str) <<std::endl;
  51.     return 0;
  52. }
Advertisement
RAW Paste Data Copied
Advertisement