首页 > 其他 > 详细

CF271D Good Substrings

时间:2021-05-20 15:45:57      阅读:16      评论:0      收藏:0      [点我收藏+]

原题链接

  • 题意:给出 \(|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();    
}

CF271D Good Substrings

原文:https://www.cnblogs.com/Xiao-yan/p/14788721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!