返回题解分享
讨论 / 题解分享/ 帖子详情

子串简写(编程题) - 题解

树状数组

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>

#define int long long
#define lowbit(x) x & -x;

using namespace std;
typedef pair<int,int> PII;
const int INF = 1e9,N = 1e6 + 10;
int tr[N];
int n;

void change(int x,int val)
{
    while(x <= n) tr[x] += val,x += lowbit(x);
}

int query(int x)
{
    int t = 0;
    while(x) t += tr[x],x -= lowbit(x);
    return t;
}

void solve()
{
    int k;
    cin >> k;
    string s;
    cin >> s;
    n = s.size();
    char l,r;
    cin >> l >> r;
    int ans = 0;
    for(int i = 0;i < n;i++)
    {
        if(s[i] == l) change(i + k - 1,1);
        if(s[i] == r) ans += query(i);
    }
    cout << ans << endl;
}

signed main()
{
    cin.tie(nullptr),ios :: sync_with_stdio(false),cout.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t--) solve();
    system("pause");
    return 0;
}
0 回复 0 转发 0 喜欢 4 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!