data structures dsu graphs

Please click on ads to support us..

Python Code:

in_il = lambda: list(map(int, input().split()))
in_sl = lambda: input().split()
in_i = lambda: int(input())
MAX_INT = float('inf')

def solve():
    n = in_i()
    s = input()
    ans = 1
    for i in range(1, len(s)):
        if s[i] == "(" and s[i-1] == "(":
            ans += 1
    print(ans)

for _ in range(in_i()):
    solve()
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define setbitcounts(x)   __builtin_popcountll(x)
#define ll                long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define pll               pair<long,long>
#define vii               vector<int,int>
#define vll               vector<long,long>
#define mii               map<int,int>
#define mci               map<char,int>
#define sti               set<int>
#define stc               set<char>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define rep2(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define mem0(a)           memset(a,0,sizeof(a))
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppcll             __builtin_popcountll
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
unordered_map<long long, int, custom_hash> safe_map;

// works for at least up to (2**31 &mdash; 1)**2
long long int_sqrt (long long x) {
    long long ans = 0;
    for (ll k = 1LL << 30; k != 0; k /= 2) {
    if ((ans + k) * (ans + k) <= x) {
        ans += k;
       }
    }
    return ans;
}

// Power function using modular exponentiation
ll powmod(ll a, ll b, ll p){
    a %= p;
    if (a == 0) return 0;
    ll product = 1;
    while(b > 0){
        if (b&1){    // you can also use b % 2 == 1
            product *= a;
            product %= p;
            --b;
        }
        a *= a;
        a %= p;
        b /= 2;    // you can also use b >> 1
    }
    return product;
}


const long long INF=1e18;
const int32_t M32=1e9+7;
const int32_t MM32=998244353;
const int N=0;
//ascii value A=65,Z=90,a=97,z=122

void solve(){     

}


// Abe saale always use long long, never ever use int in CP, 
// Tuzhe two times dokha diya hai isne yaad rakh saale.
// Also for Min and Max always use INT_MAX and INT_MIN respectively.
// Learn from your mistake pagal, kitane baar katvayega. 
// Give more time and focus on prob B in div2 as off now, 
// think again and again with pen and paper before coding it


// Always use 1 based index for prefix sum problems
// 1 based for all that it for given input vector too.
// Means store all elements from 1 to n only
// before 1 or after n we can have 0
// This is most imp : Always remember to use 1 based index
// in Prefix Sum problems otherwise it can get very messy
// Don't ever use in-built sqrt for CP, find squre root,
// using Binary search for large numbers


int main()
{
IOS
int t=1;
cin>>t;
while(t--){
//solve();
ll n; cin >> n;
string s; cin >> s;

if(n == 1){
    cout << 1 << endl;
    continue;
}

ll sz = s.size();
ll ans = 0;
rep(i,0,sz-1){
    if(s[i] == '(' and s[i+1] == '(')ans++;
}

cout << ans+1 << endl;

}

return 0;
}

/*
9
(())(((())))(()())


1
3
(()())


1
9
((())((()())()))()


*/


Comments

Submit
0 Comments
More Questions

318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins