1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

using namespace std;

int main()
{
    int t, n, pocz, kon, max_sr, curr;
    char input;
    bool ifPocz;
    int a,b,c;

    cin >> t;

    for(int i = 0; i < t; i++)
    {
        ifPocz = true;
        pocz = kon = max_sr = curr = 0;
        cin >> n;
        for(int j = 0; j < n; j++)
        {
            cin >> input;
            if(input == '1')
            {
                if(ifPocz)
                {
                    ifPocz = false;
                    pocz = curr;
                    curr = 0;
                }
                else
                {
                    max_sr = max(max_sr, curr);
                    curr = 0;
                }
            }
            else
                curr++;
        }
        kon = curr;

        a = max(pocz, kon);
        b = max(min(pocz, kon) - 1, 0);
        if(a > 0 && b > 0)
            c = max(max_sr - 4, 0);
        else if(a > 0)
            c = max(max_sr - 2, 0);
        else    // a = 0, b = 0
            c = max_sr - 1;
        
        cout << n - (a + b + c) << endl;
    }
    return 0;
}