Codeforces 165C Another Problem on Strings two minutes

Source: Internet
Author: User

Topic Link: codeforces 165C Another Problem on Strings

C. Another Problem on Strings
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
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 are read starting from some position in string W. F or example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are. So, if some string occurs multiple times, we should consider it the number of it occurs.

You are given a binary string s. Your task is to find the number of it substrings, containing exactly k characters "1".

Input
The "The" the contains the single integer k (0≤k≤106). The second line contains a non-empty binary string s. The length of s does not exceed characters.

Output
Print the single number-the number of substrings of the given string, containing exactly k characters "1".

Please don't use the%lld specifier to read or write 64-bit integers inс++. It is preferred to use the CIN, cout streams or the%i64d specifier.

Examples
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note
In the the sought substrings are: "1", "1", "10", "01", "10", "010".

In the second sample the sought substrings are: "101", "0101", "1010", "01010".

Q: Ask you to have a strict number of K 1 substring.

Idea: preprocessing prefixes and, then two points.

AC Code:

#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include < cstring> #include <queue> #define CLR (A, B) memset (A, (b), sizeof (a)) #define FI #define SE second using NA
Mespace std;
typedef long Long LL;
typedef pair<int, int> PII;
const int MAXN = 1e6 +10;
const int INF = 0X3F3F3F3F;
const int MOD = 1e9 + 7;
void Add (ll &x, ll y) {x + y; x%= MOD;} char STR[MAXN];
int SUM[MAXN];
    int find (int l, int r, int p, int v) {int ans = R + 1;
        while (R >= l) {int mid = (L + r) >> 1;
            if (Sum[mid]-sum[p-1] >= v) {ans = mid;
        R = mid-1;
        else {L = mid + 1;
} return ans;
    int main () {int k;
        while (scanf ("%d", &k)!= EOF) {scanf ("%s", str+1); int len = strlen (str+1);
        Sum[0] = 0;
        for (int i = 1; I <= len i++) {Sum[i] = Sum[i-1] + (str[i] = = ' 1 ');
    }    LL ans = 0;
            for (int i = 1; I <= len i++) {int s = find (i, Len, I, K);
            if (s = = len + 1) continue;
            int t = find (i, Len, I, k+1);
        Ans = t-s;
    printf ("%lld\n", ans);
return 0; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.