String Deletion (Codeforces 1430D)
Tags: Greedy, two pointers

You have a string $$$s$$$ consisting of $$$n$$$ characters. Each character is either 0 or 1.You can perform operations on the string. Each operation consists of two steps:  select an integer $$$i$$$ from $$$1$$$ to the length of the string $$$s$$$, then delete the character $$$s_i$$$ (the string length gets reduced by $$$1$$$, the indices of characters to the right of the deleted one also get reduced by $$$1$$$);  if the string $$$s$$$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed.For example, if you have a string $$$s =$$$ 111010, the first operation can be one of the following:  select $$$i = 1$$$: we'll get 111010 $$$\rightarrow$$$ 11010 $$$\rightarrow$$$ 010;  select $$$i = 2$$$: we'll get 111010 $$$\rightarrow$$$ 11010 $$$\rightarrow$$$ 010;  select $$$i = 3$$$: we'll get 111010 $$$\rightarrow$$$ 11010 $$$\rightarrow$$$ 010;  select $$$i = 4$$$: we'll get 111010 $$$\rightarrow$$$ 11110 $$$\rightarrow$$$ 0;  select $$$i = 5$$$: we'll get 111010 $$$\rightarrow$$$ 11100 $$$\rightarrow$$$ 00;  select $$$i = 6$$$: we'll get 111010 $$$\rightarrow$$$ 11101 $$$\rightarrow$$$ 01. You finish performing operations when the string $$$s$$$ becomes empty. What is the maximum number of operations you can perform?

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.The second line contains string $$$s$$$ of $$$n$$$ characters. Each character is either 0 or 1.It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, print a single integer — the maximum number of operations you can perform.

Note: In the first test case, you can, for example, select $$$i = 2$$$ and get string 010 after the first operation. After that, you can select $$$i = 3$$$ and get string 1. Finally, you can only select $$$i = 1$$$ and get empty string.

Sample Inputs:
5
6
111010
1
0
1
1
2
11
6
101010

Sample Outputs:
3
1
1
1
3

Solution (Python):
from collections import deque
for _ in range(int(input())):
    n = int(input())
    s = input()
    q = deque()
    cur = 0
    for i in range(n):
        if i > 0 and s[i] != s[i - 1]:
            cur += 1
        if i > 0 and s[i] == s[i - 1]:
            q.append(cur)
    delet = 0
    score = 0
    i = 0
    while i < n:
        if not q:
            break
        q.popleft()
        delet += 1
        score += 1
        while q and q[0] == i:
            q.popleft()
            delet += 1
        i += 1
        delet += 1
    score += (n - delet + 1) // 2
    print(score)

Solution (Python):
def ss(n, s):
    if n == 1:
        return 1
    d = {}
    i = 0
    j = 0
    ans = 0
    while i < n:
        while j + 1 < n and s[j] != s[j + 1]:
            j += 1
        if j >= n - 1:
            i += 1
            while i in d:
                i += 1
            i += 1
            while i in d:
                i += 1
        else:
            d[j] = 1
            x = s[i]
            i += 1
            while i in d or (i < n and s[i] == x):
                i += 1
        ans += 1
        j = max(i, j + 1)
    return ans
for _ in range(int(input())):
    n = int(input())
    s = str(input())
    print(ss(n, s))

Solution (Python):
import sys
import math
original_stdout = sys.stdout
var_for_sublime = False
if var_for_sublime:
    fin = open('input.txt', 'r')
    fout = open('output.txt', 'w')
    sys.stdout = fout

    def give_string():
        return fin.readline()
else:
    fin = sys.stdin
    fout = sys.stdout

    def give_string():
        return fin.buffer.readline()

def give_list():
    return list(map(int, give_string().split()))

def give_int():
    return int(give_string())
MOD = 1000000000.0 + 7

def modpow(a, p):
    ans = 1
    while p:
        if p & 1:
            ans *= a
            ans %= MOD
        p >>= 1
        a *= a
        a %= MOD
    return ans

def fac_cal(n):
    ans = 1
    for i in range(1, n + 1):
        ans *= i
        ans %= MOD
    return ans

def solve():
    t = 1
    n = give_int()
    s = give_string()
    i = 0
    ans = 0
    c10 = 0
    while i < n:
        temp = s[i]
        co = 0
        while i < n and temp == s[i]:
            co += 1
            i += 1
        if co == 1:
            c10 += 1
        elif c10 >= co:
            ans += co - 1
            c10 -= co - 1
            c10 += 1
        else:
            co -= c10
            ans += c10
            if co == 1:
                c10 = 1
            else:
                ans += 1
                c10 = 0
    ans += math.ceil(c10 / 2.0)
    print(int(ans))

def Hey_Yashwant():
    try:
        t = 1
        t = give_int()
        for i in range(t):
            solve()
    finally:
        fin.close()
        fout.close()
        sys.stdout = original_stdout
if __name__ == '__main__':
    Hey_Yashwant()