Loading

HDU-5672 String

String

给出字符串,问有多少个子串包含n个不同的字母

尺取模板

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int alp[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int n;
        cin >> n;
        for(int i='a'; i<='z'; i++) alp[i] = 0;
        int l = 0, r = 0, now = 0;
        ll ans = 0;
        while(1)
        {
            while(r < s.length() && now < n)
            {
                if(++alp[s[r++]] == 1)
                    now++;
            }
            if(now < n) break;
            ans += s.length() - r + 1;
            if(--alp[s[l++]] == 0) now--;
        }
        cout << ans << endl;
    }

    return 0;
}
posted @ 2022-05-13 01:24  dgsvygd  阅读(16)  评论(0编辑  收藏  举报