Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7

mem = {}


def solve(pre, s):
    if s in mem:
        res = mem[s]
        res_pre, res_c = res
        if pre != res_pre:
            return res

    if len(s) == 1:
        if pre != s:
            return s, 1
        return None
    for i in range(1, len(s)):
        head = s[:i]
        if pre == head:
            continue
        r = solve(head, s[i:])
        if r is not None:
            r_pre, r_c = r
            mem[s] = r
            return r_pre, r_c + 1
    if pre != s:
        return s, 1


def main():
    s = input()
    # N = int(input())
    print(solve("", s)[1])
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()


Fixed submission:
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def f(n):
    if n == 0:
        return 0, False
    a = n // 3
    b = n % 3
    if b == 0:
        return a * 2, 0
    if b == 1:
        return a * 2 + 1, 1

    return a * 2 + 1, 2


def solve(s):
    last = s[0]
    c = 1
    res = 0
    last_flag = False
    for cc in s[1:]:
        if last == cc:
            c += 1
            continue

        k, k_flag = f(c)
        if k_flag == 0:
            res += k
        elif k_flag == 1:
            res += k
            last_flag = False
        else:
            if last_flag:
                res += k + 1
                last_flag = False
            else:
                res += k
                last_flag = True
        c = 1
        last = cc
    k, k_flag = f(c)
    if k_flag == 0:
        res += k
    elif k_flag == 1:
        res += k
    else:
        if last_flag:
            res += k + 1
        else:
            res += k
    return res


def main():
    s = input()
    # N = int(input())
    print(solve(s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def solve(pre, s):
    if len(s) == 1:
        if pre != s:
            return 1
        return None
    for i in range(1, len(s)):
        head = s[:i]
        if pre == head:
            continue
        r = solve(head, s[i:])
        if r is not None:
            return r + 1
    if pre != s:
        return 1


def main():
    s = input()
    # N = int(input())
    print(solve("", s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()


Fixed submission:
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def f(n):
    if n == 0:
        return 0, False
    a = n // 3
    b = n % 3
    if b == 0:
        return a * 2, 0
    if b == 1:
        return a * 2 + 1, 1

    return a * 2 + 1, 2


def solve(s):
    last = s[0]
    c = 1
    res = 0
    last_flag = False
    for cc in s[1:]:
        if last == cc:
            c += 1
            continue

        k, k_flag = f(c)
        if k_flag == 0:
            res += k
        elif k_flag == 1:
            res += k
            last_flag = False
        else:
            if last_flag:
                res += k + 1
                last_flag = False
            else:
                res += k
                last_flag = True
        c = 1
        last = cc
    k, k_flag = f(c)
    if k_flag == 0:
        res += k
    elif k_flag == 1:
        res += k
    else:
        if last_flag:
            res += k + 1
        else:
            res += k
    return res


def main():
    s = input()
    # N = int(input())
    print(solve(s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def solve(pre, s):
    if len(s) == 1:
        if pre != s:
            return 1
        return None
    for i in range(1, len(s)):
        head = s[:i]
        if pre == head:
            continue
        r = solve(head, s[i:])
        if r is not None:
            return r + 1
    if pre != s:
        return 1


def main():
    s = input()
    # N = int(input())
    print(solve("", s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()


Fixed submission:
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def f(n):
    if n == 0:
        return 0, False
    a = n // 3
    b = n % 3
    if b == 0:
        return a * 2, 0
    if b == 1:
        return a * 2 + 1, 1

    return a * 2 + 1, 2


def solve(s):
    last = s[0]
    c = 1
    res = 0
    last_flag = False
    for cc in s[1:]:
        if last == cc:
            c += 1
            continue

        k, k_flag = f(c)
        if k_flag == 0:
            res += k
        elif k_flag == 1:
            res += k
            last_flag = False
        else:
            if last_flag:
                res += k + 1
                last_flag = False
            else:
                res += k
                last_flag = True
        c = 1
        last = cc
    k, k_flag = f(c)
    if k_flag == 0:
        res += k
    elif k_flag == 1:
        res += k
    else:
        if last_flag:
            res += k + 1
        else:
            res += k
    return res


def main():
    s = input()
    # N = int(input())
    print(solve(s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

input = sys.stdin.readline

sys.setrecursionlimit(10000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def solve(pre, s):
    if len(s) == 1:
        if pre != s:
            return 1
        return None
    for i in range(1, len(s)):
        if pre == s[:i]:
            continue
        r = solve(s[:i], s[i:])
        if r is not None:
            return r + 1
    if pre != s:
        return 1


def main():
    s = input().strip()
    # N = int(input())
    print(solve("", s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()


Fixed submission:
from collections import defaultdict, deque, Counter
import sys
import heapq
import math

# input = sys.stdin.readline

sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


def f(n):
    if n == 0:
        return 0, False
    a = n // 3
    b = n % 3
    if b == 0:
        return a * 2, 0
    if b == 1:
        return a * 2 + 1, 1

    return a * 2 + 1, 2


def solve(s):
    last = s[0]
    c = 1
    res = 0
    last_flag = False
    for cc in s[1:]:
        if last == cc:
            c += 1
            continue

        k, k_flag = f(c)
        if k_flag == 0:
            res += k
        elif k_flag == 1:
            res += k
            last_flag = False
        else:
            if last_flag:
                res += k + 1
                last_flag = False
            else:
                res += k
                last_flag = True
        c = 1
        last = cc
    k, k_flag = f(c)
    if k_flag == 0:
        res += k
    elif k_flag == 1:
        res += k
    else:
        if last_flag:
            res += k + 1
        else:
            res += k
    return res


def main():
    s = input()
    # N = int(input())
    print(solve(s))
    # N, A, B = [int(a) for a in input().split()]
    # A = [
    #     [int(a) for a in input().split()]
    #     for _ in range(N)
    # ]


if __name__ == '__main__':
    main()