Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://codeforces.com/problemset/problem/165/C
- A string is binary, if it consists only of characters "0" and "1".
- String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
- You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
- Examples
- input
- 1
- 1010
- output
- 6
- input
- 2
- 01010
- output
- 4
- input
- 100
- 01010
- output
- 0
- Note
- In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
- In the second sample the sought substrings are: "101", "0101", "1010", "01010".
- ----------------------------------------------------------------------------------------------------------------------
- #include<bits/stdc++.h>
- using namespace std;
- void solve(){
- int k;
- cin>>k;
- string s;
- cin>>s;
- long long int sum=0;
- long long int res=0;
- unordered_map<long long int,long long int> mpp;
- for(auto x: s){
- sum+=(x=='1' ? 1 : 0);
- if(sum==k){
- res++;
- }
- if(mpp.find(sum-k)!=mpp.end()){
- res+=mpp[sum-k];
- }
- mpp[sum]++;
- }
- cout<<res;
- }
- int main(){
- int TC=1;
- while(TC--){
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment