solution.cpp 1.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <bits/stdc++.h>
using namespace std;
int len;
string s;
bool check(int i)
{
    if (len - i < 5)
        return false;
    return s[i + 1] == 'l' && s[i + 2] == 'i' && s[i + 3] == 'c' && s[i + 4] == 'e';
}
bool check2(int i)
{
    if (len - i < 3)
        return false;
    return s[i + 1] == 'o' && s[i + 2] == 'b';
}
int main()
{
    int k; //这里绝对不能加关闭流读入,如果这加了getline会直接读不到
    cin >> k;
    getchar();
    getline(cin, s);
    len = s.length();
    vector<int> Alice, Bob;
    for (int i = 0; i < len; i++)
    {
        if (s[i] == 'A' && check(i))
        {
            Alice.push_back(i);
            i += 5;
        }
        else if (s[i] == 'B' && check2(i))
        {
            Bob.push_back(i);
            i += 3;
        }
    }
    int As = Alice.size(), Bs = Bob.size();
    int i = 0, j = 0;
    long long ans = 0;
    for (int q = 0; q < As; q++)
    {
        while (i < Bs && Bob[i] < Alice[q] - k - 3)
            i++; //左边界已经有些被排除的
        while (j < Bs && Bob[j] <= Alice[q] + k + 5)
            j++; //右边界
        ans += j - i;
    }
    cout << ans << "\n";
    return 0;
}