其他分享
首页 > 其他分享> > HDU-5672 String

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;
}

标签:HDU,const,String,int,ll,cin,5672,include
来源: https://www.cnblogs.com/dgsvygd/p/16265199.html