Rooms and Staircases (Codeforces 1244B)
Tags: Implement, brute force

Nikolay lives in a two-storied house. There are $$$n$$$ rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between $$$1$$$ and $$$n$$$). If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers $$$i$$$ and $$$i+1$$$ on each floor are neighbouring, for all $$$1 \leq i \leq n - 1$$$. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room $$$x$$$ on the first floor and the room $$$x$$$ on the second floor, then Nikolay can use it to move from one room to another.   The picture illustrates a house with $$$n = 4$$$. There is a staircase between the room $$$2$$$ on the first floor and the room $$$2$$$ on the second floor, and another staircase between the room $$$4$$$ on the first floor and the room $$$4$$$ on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string "0101" in the input.  Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). Calculate the maximum number of rooms Nikolay can visit during his tour, if:  he can start in any room on any floor of his choice,  and he won't visit the same room twice. 

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines.The first line contains one integer $$$n$$$ $$$(1 \le n \le 1\,000)$$$ — the number of rooms on each floor.The second line contains one string consisting of $$$n$$$ characters, each character is either a '0' or a '1'. If the $$$i$$$-th character is a '1', then there is a staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. If the $$$i$$$-th character is a '0', then there is no staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor.In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied.

Output: For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.

Note: In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits $$$6$$$ rooms.There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).In the third test case it is possible to visit all rooms: first floor, first room $$$\rightarrow$$$ second floor, first room $$$\rightarrow$$$ second floor, second room $$$\rightarrow$$$ first floor, second room $$$\rightarrow$$$ first floor, third room $$$\rightarrow$$$ second floor, third room $$$\rightarrow$$$ second floor, fourth room $$$\rightarrow$$$ first floor, fourth room $$$\rightarrow$$$ first floor, fifth room $$$\rightarrow$$$ second floor, fifth room.In the fourth test case it is also possible to visit all rooms: second floor, third room $$$\rightarrow$$$ second floor, second room $$$\rightarrow$$$ second floor, first room $$$\rightarrow$$$ first floor, first room $$$\rightarrow$$$ first floor, second room $$$\rightarrow$$$ first floor, third room.

Sample Inputs:
4
5
00100
8
00000000
5
11111
3
110

Sample Outputs:
6
8
10
6

Solution (Python):
from itertools import permutations

def allPermutations(str):
    permList = permutations(str)
    return permList

def factors(x):
    result = []
    i = 1
    while i * i <= x:
        if x % i == 0:
            result.append(i)
            if x // i != i:
                result.append(x // i)
        i += 1
    return result

def fast_expo(val, power):
    result = pow(val, power // 2)
    result = result * result
    if power % 2 != 0:
        result = result * val
    return result

def is_prime(n):
    if n < 2:
        return False
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True

def transpose(l1, l2):
    for i in range(len(l1[0])):
        row = []
        for item in l1:
            row.append(item[i])
        l2.append(row)
    return l2

def primeFactor(x):
    l = []
    while x % 2 == 0:
        l.append(2)
        x = x // 2
    for i in range(3, int(math.sqrt(x) + 1), 2):
        while x % i == 0:
            l.append(i)
            x = x / i
    if x > 2:
        l.append(x)
    return l
from math import gcd as bltin_gcd

def coprime2(a, b):
    return bltin_gcd(a, b) == 1

def sieve_of_eratosthenes(limit):
    primes = [True] * (limit + 1)
    primes[0] = primes[1] = False
    for i in range(2, int(limit ** 0.5) + 1):
        if primes[i]:
            for j in range(i * i, limit + 1, i):
                primes[j] = False
    return primes
import math
from collections import *
from itertools import groupby
import heapq
for _ in range(int(input())):
    n = int(input())
    s = input()
    l = 0
    h = n - 1
    ans = 0
    while l <= h:
        if s[l] == '1':
            ans = (n - l) * 2
            break
        elif s[h] == '1':
            ans = (h + 1) * 2
            break
        else:
            l += 1
            h -= 1
    print(max(ans, n))

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    s = input()
    i = -1
    for x in range(n):
        if s[x] == '1':
            i = x
            break
    if i != -1:
        for x in range(n - 1, -1, -1):
            if s[x] == '1':
                j = x
                break
        m = max(n - i, 1 + j)
        print(m * 2)
    else:
        print(n)

Solution (Python):
for t in range(int(input())):
    n = int(input())
    x = input()
    l = len(x)
    if '1' not in x:
        print(*[l])
    elif l % 2 == 0:
        a = x.index('1')
        b = x[::-1]
        c = b.index('1')
        if a <= c:
            pass
        else:
            a = c
        if a < l // 2:
            print(*[l - a + l - a])
        else:
            print(*[a + a + 2])
    else:
        a = x.index('1')
        b = x[::-1]
        c = b.index('1')
        if a <= c:
            pass
        else:
            a = c
        if a <= l // 2:
            print(*[l - a + l - a])
        else:
            print(*[a + a + 2])