CF271D Good Substrings

原题链接

  • 题意:给出 (|s| lesqlant 1500) 并且给出哪些字母是好哪些是坏,然后要求求出一共有多少本质不同的字串,使得坏串个数不超过 (k) 个。
  • 题解:显然可以直接 (n^2) 暴力找然后,用字符串 (Hash) 判重。
  • 代码:
#include <bits/stdc++.h>

using namespace std;


typedef long long ll;
typedef unsigned long long ull;
const ll P = 131;
char str[2020];
bool good[200];
ull Hash[2020];
int sum[2020];
ull p[2020];
map<ull, ll>mp;
void solve() {
    cin >> (str + 1);
    string s;cin >> s;
    for (int i = 0; i < s.size(); i ++) {
        if (s[i] == '1') good['a' + i] = 1; 
    }
    int k;cin >> k;
    Hash[0] = 0;
    p[0] = 1;
    int n = strlen(str + 1);
    for (int i = 1; i <= n; i ++) {
        Hash[i] = Hash[i-1] * P + str[i];
        p[i] = p[i-1] * P; 
        sum[i] = sum[i-1];
        if (!good[str[i]])sum[i]++;
    }
    ll ans = 0;
    for (int i = 1; i <=n; i ++) {
        for (int j = i ; j <= n; j ++) {
            ull x = Hash[j] -Hash[i-1] * p[(j - (i - 1))];
            if (sum[j] - sum[i-1] <= k) {
                if (!mp.count(x)) {
                    //cout << i << " " << j << endl;
                    mp[x] = 1;
                    ans++;
                }
            }
        }
    }
    cout << ans << endl;
}
int main() {
    int t = 1;//cin >> t;
    while (t--)solve();    
}
原文地址:https://www.cnblogs.com/Xiao-yan/p/14788721.html