Codeforces 165C Another Problem on Strings(尺取法)

题目

题目链接Problem - 165C - Codeforces

        

C. Another Problem on Strings

2 seconds   256 megabytes

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".

Input

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

Output

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

Please do not 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.

题意

        可能讲不清楚,大致就是给出k和字符串st,求包含正好有k个1的st子串有多少个,子串不同位置的话算不同的子串,也就是比如st="001110001",k=2,st="001110001"和st="001110001"是不一样的。

思路

        我们用尺取法,顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案。

        举例st=“001110001”,k=3,我们要从左往右扫,扫到有k个1的时候就判断这个区间最左边的1往左边有多少个0,最右边的1往右边有多少个0,这样得到的(suml+1)*(sumr+1)就是基于有k个1的情况下左右添0的个数(至于为什么可以手推),然后我们不断将这把“尺子”往右边移,寻找下一个满足k=3的尺子。

        思路是这样的,我将其进行用vector优化,将vector存储每一处st[i]=1的位置,并将最前面和最后面手动添临界情况,即st前后也有一个1,这样的目的是相邻的vector数组相减就是坐标值差,也就是中间0的个数(算上+1了,因为3-1=2,但是中间只有一个0,正好+1)

        然后注意for循环的终止条件,最右端不要到手动添加地方,左右都得保证能够有相邻的可以处理。

代码

#include<stack>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<deque>
#include<vector>
#include<iostream>
#include<map>
#include<set>
#include<iomanip>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
int l,r,ans,len,k,num;
vector<int> v;
string st;
signed main() 
{
    #ifndef ONLINE_JUDGE
		    freopen("IO\\in.txt","r",stdin);
		    freopen("IO\\out.txt","w",stdout);
        #endif
    IOS
    cin>>k>>st;
    len=st.length();
    if (k==0)
    {
        for (int i=0;i<len;i++)
        {
            if (st[i]=='1') ans+=num*(num+1)/2,num=0;
            else
            {
                num++;
            }
        }
        ans+=num*(num+1)/2;
        cout<<ans;
        return 0;
    }
    else
    {
        v.push_back(-1);
        for (int i=0;i<len;i++)
            if (st[i]=='1') v.push_back(i);
        v.push_back(len);
        for (int i=1;i+k-1<v.size()-1;i++)
        {
            l=i;r=i+k-1;
            ans+=(v[l]-v[l-1])*(v[r+1]-v[r]);
        }
        cout<<ans;
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值