转01串

问题 I: Tired Terry

时间限制: 1 Sec  内存限制: 128 MB
提交 状态

题目描述

Terry is feeling tired and he suspects it is because of a lack of sleep. He created a device that records his sleeping pattern over a period of time measured in seconds.
Assuming that the recorded sleeping pattern keeps repeating, help Terry by letting him know how often he is tired during each of the repeating time periods.
More precisely, for integers p and d, we say that Terry is tired at second i if from second i − p + 1 to second i (inclusive) he has slept for less than d seconds.

输入

The first line of input contains three integers n (1 ≤ n ≤ 86 400), the length of Terry’s sleep pattern, p (1 ≤ p ≤ N), and D (1 ≤ d ≤ p) as described above.
The second line of input contains a single string of length n which describes the period of time that is recorded. The ith such character is a W if Terry is awake at the ith second, or is a Z if Terry is asleep at the ith second.

输出

Display a single integer which represents the number of seconds that Terry is tired during each of the repeating time periods.

样例输入 Copy

【样例1】
2 1 1
WZ
【样例2】
5 3 2
WZWWZ

样例输出 Copy

【样例1】
1
【样例2】
4
就是说给你一串音乐,是无限循环的,W代表这醒着,Z代表这睡着了,如果在某一时刻,i到i-p+1的睡的时刻小于d,就代表这这一时刻他疲劳,就是
问你在这在这个循环中那一时刻是一直疲劳的(每一次循环到这就疲劳)
就是转换字符串为01字符串,这样就能用前缀求出他在这一区间内的疲劳时刻
就是把w->1,z->0,那么一段区间内的疲劳度就是
s[i] - s[i - p]

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int N = 1000010;
int s[N], sum[N];
int main()
{
    int n, p , d;
    string a;
    cin >> n >> p >> d;
    cin >> a;
    int k = a.size();
    a = a + a;
    for (int i = 0;i < a.size();i++)
    {
        if (a[i] == 'Z') s[i + 1] = s[i] + 1;
        else {
            s[i + 1] = s[i];
        }
    }
    int ans = 0;
    for (int i = k + 1;i <= a.size();i++)
    {
        if (s[i] - s[i - p] < d) ans++;
    }
    cout << ans << endl;
    return 0;
}




原文地址:https://www.cnblogs.com/lipu123/p/13658424.html