Submission #46670291


Source Code Expand

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag, tree_order_statistics_node_update>oset; 
#define hi cout << "test\n" ;/////
const int mod = 998244353 ;
#define all(cv) cv.begin(),cv.end() 
#define rall(cv) cv.rbegin() ,cv.rend() 
#define  ll unsigned long long
const ll INF = 1e18 ; 
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" = "; _print(x); cerr << endl;
#else
#define dbg(x)
#endif
ll binpow(ll a, ll b) {if (b == 0)return 1; ll res = binpow(a, b / 2);if (b % 2) return res * res * a;else return res * res;}
ll expo(ll a, ll b, ll mod) {ll ans = 1; while (b > 0) {if (b & 1)ans = (ans * a) % mod; a = (a * a) % mod; b = b >> 1;} return ans;}
ll gcd(ll a, ll b) { return((b == 0) ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return (b / gcd(a, b)) * a; }
void _print(auto t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template < typename T > void output (const vector<T> & vec ){  for(auto elem : vec) cout<<elem<<" ";cout <<"\n" ;}
//--------------------------------------------------------------- ------------------------------------------------------------------- 
int rec ( int index , int cnt , int valid , string s , vector < vector < vector < int > > > &dp ) {
     if ( cnt < 0 ) return 0 ; 
     if ( index == s.size()  ){
       return cnt == 0 and valid ;
     }
   if ( dp[index][cnt][valid] != -1 ) return dp[index][cnt][valid] % mod ;   
   int ans = 0;   
   if ( s[index] == '(' ) 
      ans =  rec ( index + 1 , cnt + 1 , 0 , s ,dp) ;
   else if ( s[index] == ')' )
      ans =  rec ( index + 1 , cnt - 1 , 1 , s ,dp) ; 
    else {
        int a = rec ( index + 1 , cnt + 1 , 0 , s ,dp) % mod;
        int b = rec ( index + 1 , cnt - 1 , 1 , s ,dp) % mod ;
        ans = a + b ; 
    }
    return dp[index][cnt][valid] = ans % mod  ; 
}
void solve(){

    string s ;
    cin >> s ; 
    vector < vector < vector < int >>> dp (s.size() + 1, vector<vector<int>>(s.size() + 1, vector<int>(2, -1)));
    cout << rec ( 0 , 0 , 0 , s , dp) ; 


}
//---------------------------------------------------------------------------------------------------------------------------------
int32_t main(){
 #ifndef ONLINE_JUDGE
    freopen("error.txt","w",stderr) ;
 #endif 


     FastIO ;
     int test = 1;
     int TestCases = 0  ; 

     if ( TestCases )
             cin>>test; 

     while(test--){
     solve(); 
      if(test>0)
        cout << endl; 
     }
 
 }      

Submission Info

Submission Time
Task D - Count Bracket Sequences
User Moorit
Language C++ 20 (gcc 12.2)
Score 0
Code Size 3484 Byte
Status TLE
Exec Time 2242 ms
Memory 505392 KiB

Compile Error

Main.cpp: In function ‘int rec(int, int, int, std::string, std::vector<std::vector<std::vector<int> > >&)’:
Main.cpp:37:17: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   37 |      if ( index == s.size()  ){
      |           ~~~~~~^~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 15
TLE × 9
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt
Case Name Status Exec Time Memory
sample00.txt AC 1 ms 3436 KiB
sample01.txt AC 1 ms 3472 KiB
sample02.txt AC 1 ms 3624 KiB
testcase00.txt AC 1 ms 3532 KiB
testcase01.txt AC 1 ms 3428 KiB
testcase02.txt AC 1 ms 3436 KiB
testcase03.txt AC 956 ms 171888 KiB
testcase04.txt AC 1395 ms 222144 KiB
testcase05.txt TLE 2242 ms 505236 KiB
testcase06.txt TLE 2228 ms 371288 KiB
testcase07.txt TLE 2239 ms 505392 KiB
testcase08.txt AC 827 ms 175876 KiB
testcase09.txt TLE 2239 ms 505332 KiB
testcase10.txt TLE 2230 ms 406556 KiB
testcase11.txt TLE 2239 ms 505348 KiB
testcase12.txt AC 413 ms 140992 KiB
testcase13.txt TLE 2026 ms 505344 KiB
testcase14.txt AC 381 ms 219528 KiB
testcase15.txt AC 472 ms 505356 KiB
testcase16.txt TLE 2230 ms 400228 KiB
testcase17.txt TLE 2239 ms 504996 KiB
testcase18.txt AC 388 ms 201332 KiB
testcase19.txt AC 175 ms 216852 KiB
testcase20.txt AC 409 ms 495952 KiB