對於每一個 \(f(i, j)\),我們考慮如何計算。我們發現,\(\texttt{1010}\) 式的字串很有用,所以這啟發我們如果遇到了一個模式 \(p_i = \texttt{'1'}\),那麼我們可以在 \(i + 1\) 的位置放一個 \(\texttt{'1'}\)。這樣我們直接處理了 \(i, i + 1, i + 2\)。容易證明這是最優的。
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
int read() {
char c = getchar();
int x = 0, p = 1;
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') p = -1, c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * p;
}
int n;
string s;
int f(string p) {
int res = 0;
for (int i = 0; i < (int) p.size(); i ++) {
if (p[i] == '1') {
res ++;
i += 2;
}
}
return res;
}
void solve() {
n = read();
cin >> s; long long ans = 0;
for (int i = 0; i < n; i ++) {
string t = "";
for (int j = i; j < n; j ++) {
t += s[j];
ans += f(t);
}
}
cout << ans <<'\n';
}
signed main() {
int t = read();
while (t --) solve();
return 0;
}