题解
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[1000005] = {0};
void solve() {
ll n, ans = 0;
cin >> n;
string s;
cin >> s;
s = " " + s; // 使字符串1索引化
for (ll i = 1; i <= n; i++) {
if (s[i] == '1') {
if (i > 3) {
dp[i] = 1 + 1 + 1 + dp[i-3] + 1*(i-3);
}
else dp[i] = i;
}
else dp[i] = dp[i-1];
ans += dp[i];
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
cin >> t;
while (t--) solve();
return 0;
}
标签:Sum,Hard,long,Substrings,Version,ans,ll,dp
From: https://www.cnblogs.com/pure4knowledge/p/18300209