zoukankan      html  css  js  c++  java
  • 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();    
    }
    
  • 相关阅读:
    *HDU2473 并查集
    *HDU3172 并查集
    *cf.4 贪心
    *HDU3635 并查集
    *HDU1325 并查集
    *HDU1829 并查集
    Node.js 学习笔记二
    Node.js 学习笔记 一
    AngularJS 学习笔记 一
    MongoDB 基础知识二
  • 原文地址:https://www.cnblogs.com/Xiao-yan/p/14788721.html
Copyright © 2011-2022 走看看