Codeforces Round #166 (Div. 2) 271D

本文详细解析了Codeforces Round #166 (Div.2) 的 D 类题目,介绍如何通过哈希算法寻找字符串中所有满足特定条件的子串,避免双哈希超时的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces Round #166 (Div. 2) 271D

Problem Description

You’ve got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l…r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2…s|s| (where |s| is the length of string s) is string  slsl + 1…sr.
The substring s[l…r] is good, if among the letters  sl, sl + 1, …, sr there are at most k bad ones (look at the sample’s explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x…y] and s[p…q] are considered distinct if their content is different, i.e. s[x…y] ≠ s[p…q].

Input

The first line of the input is the non-empty string s, consisting of small English letters, the string’s length is at most 1500 characters.
The second line of the input is the string of characters “0” and “1”, the length is exactly 26 characters. If the i-th character of this string equals “1”, then the i-th English letter is good, otherwise it’s bad. That is, the first character of this string corresponds to letter “a”, the second one corresponds to letter “b” and so on.
The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.

Output

Print a single integer — the number of distinct good substrings of string s.

Examples

input

ababab
01000000000000000000000000
1

output

5

input

acbacbacaa
00000000000000000000000000
2

output

8

Note

In the first example there are following good substrings: “a”, “ab”, “b”, “ba”, “bab”.
In the second example there are following good substrings: “a”, “aa”, “ac”, “b”, “ba”, “c”, “ca”, “cb”.


题意

26个字母中,有一些字母是好的,一些字母是坏的。给定一个字符串和k,问这个字符串中有多少子串满足坏的字符个数不大于k。

思路

hash一下,然后O(N2)直接暴力找。

坑点

双哈希会超时。
然后第8发样例非常的dog,我WA和超时一直都在第八发样例。。。


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD1=23333333333333333;
//const ll MOD2=1e9+9;
const int N=1e7+5;
const int HS1=1313131;
//const int HS2=131;

char str[1505];
int gg[30];
map<ll,int> vis1;
//unordered_map<ll,int> vis2;

int main()
{
    scanf("%s",str+1);
    int len=strlen(str+1);
    for(int i=0; i<26; i++)
    {
        scanf("%1d",&gg[i]);
    }
    int k;
    scanf("%d",&k);
    int ans=0;
    for(int i=1;i<=len;i++)
    {
        ll hs1=0;
//        int hs2=0;
        int cnt=0;
        for(int j=i;j>=1;j--)
        {
            if(gg[str[j]-'a']==0)
                cnt++;
            if(cnt>k)
                break;
            hs1=(hs1*HS1+(str[j]-'a'+1))%MOD1;
//            hs2=(hs2*HS2+(str[j]-'a'+1))%MOD2;
            if(vis1[hs1]==0/*&&vis2[hs2]==0*/)
            {
                vis1[hs1]=1;
//                vis2[hs2]=1;
                ans++;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

内容概要:本文介绍了基于Python实现的SSA-GRU(麻雀搜索算法优化门控循环单元)时间序列预测项目。项目旨在通过结合SSA的全局搜索能力和GRU的时序信息处理能力,提升时间序列预测的精度和效率。文中详细描述了项目的背景、目标、挑战及解决方案,涵盖了从数据预处理到模型训练、优化及评估的全流程。SSA用于优化GRU的超参数,如隐藏层单元数、学习率等,以解决传统方法难以捕捉复杂非线性关系的问题。项目还提供了具体的代码示例,包括GRU模型的定义、训练和验证过程,以及SSA的种群初始化、迭代更新策略和适应度评估函数。; 适合人群:具备一定编程基础,特别是对时间序列预测和深度学习有一定了解的研究人员和技术开发者。; 使用场景及目标:①提高时间序列预测的精度和效率,适用于金融市场分析、气象预报、工业设备故障诊断等领域;②解决传统方法难以捕捉复杂非线性关系的问题;③通过自动化参数优化,减少人工干预,提升模型开发效率;④增强模型在不同数据集和未知环境中的泛化能力。; 阅读建议:由于项目涉及深度学习和智能优化算法的结合,建议读者在阅读过程中结合代码示例进行实践,理解SSA和GRU的工作原理及其在时间序列预测中的具体应用。同时,关注数据预处理、模型训练和优化的每个步骤,以确保对整个流程有全面的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值