游游有一个只包含’0’和’1’的字符串,他想知道这个字符串有多少个好子串? 一个字符串如果是"好串",那么该字符串的所有前缀,’0’的数量严格大于’1’的数量。
区块链毕设网qklbishe.com为您提供问题的解答
游游有一个只包含’0’和’1’的字符串,他想知道这个字符串有多少个好子串?
一个字符串如果是"好串",那么该字符串的所有前缀,’0’的数量严格大于’1’的数量。
importjava.util.*;
classMain {
staticfinalintMAXN = (int) (1e5 + 10);
staticchar[] chs = newchar[MAXN];
staticlongres = 0;
publicstaticvoidmain(String[] args) {
Scanner scanner = newScanner(System.in);
String s = scanner.next();
intn = s.length();
for(inti = 1; i <= n; i++) {
chs[i] = s.charAt(i – 1);
}
intcnt1 = 0, cnt0 = 0;
for(intl = 1, r = 1; r <= n; r++) {
if(chs[r] == ‘0’)
cnt0++;
else
cnt1++;
while(cnt1 >= cnt0 && l <= r) {
if(chs[l] == ‘0’)
cnt0–;
else
cnt1–;
l++;
}
res += (r – l + 1);
res -= 2L * cnt1;
}
System.out.println(res);
}
}
41:15
以上就是关于问题游游有一个只包含’0’和’1’的字符串,他想知道这个字符串有多少个好子串?
一个字符串如果是"好串",那么该字符串的所有前缀,’0’的数量严格大于’1’的数量。的答案
欢迎关注区块链毕设网-
web3一级市场套利打新赚钱空投教程
区块链NFT链游项目方科学家脚本开发培训