1657C - Bracket Sequence Deletion - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

import os
import sys 
import math
import random
from io import BytesIO, IOBase

f = 1

def isPal(s):
    i = 0
    j = len(s) - 1
    while(i < j):
        if s[i] != s[j]:
            return False
        i += 1
        j -= 1
    return True

def solve():
    n = int(input())
    s = input()
    ans = 0
    temp = ""
    i = 0
    while(i < n):
        temp += s[i]
        if len(temp) > 1:
            if isPal(temp) == True:
                ans += 1
                temp = ""
        else:
            if temp == "(" and i != n-1 and s[i+1] == ")":
                ans += 1
                temp = ""
                i += 1
        i += 1

    print(ans, len(temp))


BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if(os.path.exists('input.txt')):
    sys.stdin = open("input.txt","r")
    sys.stdout = open("output.txt","w")


if __name__ == "__main__":
    if f == 1:
        t = int(input())
        while(t):
            solve(); t -= 1
    else: solve() 

C++ Code:

#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
#define pb push_back
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std;


const int N = 2e5+5;
using LL = long long;
vector <char> st1, st2;

void solve(){
	int n;
	string s;
	cin >> n >> s;
//	cout << n << endl  << s << endl;
	st1.clear(); 
	st2.clear();
	int idx1 = 0, idx2 = 0, cnt = 0;
	while(idx2 < n){

		if(st1.empty() || st2.empty()){
			st1.pb(s[idx2]);
			st2.pb(s[idx2]);
			idx2++;
			continue;
		}
		
		if(st1.size() > 0 && s[idx2] == ')' && st1.back() == '(' )st1.pop_back();	
		else st1.pb(s[idx2]);
		
		if(s[idx2] == st2.back())st2.pop_back();
		else st2.pb(s[idx2]);
		
		if(st1.size() == 0 || st2.size() == 0 || (st2.size() == 3 && st2.front() == st2.back())){
			cnt++;
			idx1 = idx2;
			st1.clear();
			st2.clear();
		}
		idx2++;
	} 
	if(idx1 == 0)cout << 0 << " " <<n << endl;
	else cout << cnt << " " << n-idx1-1 << endl;

}

int main(){
	IOS
	int T;
	cin >> T;
	while(T--)solve();

    return 0;
}



Comments

Submit
0 Comments
More Questions

448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise