Codeforces Round #166 (Div. 2)---D. Good Substrings(字符串)

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.

Sample test(s)

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”.

字符串hash,记录bad字符个数的前缀然后暴力枚举区间即可

/*************************************************************************
    > File Name: CF-166-D.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月01日 星期三 16时11分49秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int seed = 13331;
static const int N = 1600;
static const int HASH = 10000007;

char str[N];
char good[30];
int sum[N];
unsigned long long S[N];
unsigned long long P[N];

class HASHMAP
{
    public:
        int head[HASH];
        int nxt[HASH];
        int size;
        unsigned long long state[HASH];

        void init()
        {
            memset(head, -1, sizeof(head));
            size = 0;
        }

        bool check(unsigned long long val)
        {
            int h = (val % HASH + HASH) % HASH;
            for (int i = head[h]; ~i; i = nxt[i])
            {
                if (val == state[i])
                {
                    return 1;
                }
            }
            return 0;
        }

        void insert(unsigned long long val)
        {
            int h = (val % HASH + HASH) % HASH;
            for (int i = head[h]; ~i; i = nxt[i])
            {
                if (state[i] == val)
                {
                    return;
                }
            }
            state[size] = val;
            nxt[size] = head[h];
            head[h] = size++;
        }
}H;

int main()
{
    P[0] = 1;
    for (int i = 1; i <= 1500; ++i)
    {
        P[i] = P[i - 1] * seed;
    }
    int k;
    while (~scanf("%s%s%d", str, good, &k))
    {
        S[0] = 0;
        int n = strlen(str);
        sum[0] = 0;
        for (int i = 1; i <= n; ++i)
        {
            S[i] = S[i - 1] * seed + str[i - 1];
            sum[i] = sum[i - 1] + (good[str[i - 1] - ‘a‘] == ‘0‘);
        }
        H.init();
        int ans = 0;
        for (int L = 1; L <= n; ++L)
        {
            for (int i = 1; i + L - 1 <= n; ++i)
            {
                unsigned long long val = S[i + L - 1] - S[i - 1] * P[L];
                if (!H.check(val) && sum[i + L - 1] - sum[i - 1] <= k)
                {
                    ++ans;
                    H.insert(val);
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

时间: 2024-10-05 08:12:03

Codeforces Round #166 (Div. 2)---D. Good Substrings(字符串)的相关文章

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)

解题报告 四种情况相应以下四组数据. 给两字符串,推断第一个字符串是怎么变到第二个字符串. automaton 去掉随意字符后成功转换 array 改变随意两字符后成功转换 再者是两个都有和两个都没有 #include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> #include <algorithm> #include <cmath> usi

Codeforces Round #306 (Div. 2)——A——Two Substrings

You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 an

codeforces水题100道 第十三题 Codeforces Round #166 (Div. 2) A. Beautiful Year (brute force)

题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #include <iostream> #include <algorithm> using namespace std; bool chk(int x) { int a[4]; for (int i = 0; i < 4; i ++) { a[i] = x % 10; x /= 1

Codeforces Round #166 (Div. 2)

B. Prime Matrix 题意很简单,就是求从给定矩阵基础上构造一个素数矩阵需要最少进行多少次加1操作,所谓的素数矩阵是矩阵一行或者一列全为素数.这里可以换一个思路思考,我们可以找出矩阵某行(或某列)元素距其最近的素数的差值,然后对这些差进行排序,最小的数即为所求.当然我们需要预先求一个素数数组存储起来,这也是需要学会的. 1 #include<iostream> 2 #include<algorithm> 3 #define N 1000011 4 using namesp

Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #include <iostream> #include <cstring> #define clr(x,c) memset(x,c,sizeof(x)) using namespace std; const int N = 200005; char s[N], t[N]; int sc[30],

Codeforces Round #486 (Div. 3) B. Substrings Sort

Codeforces Round #486 (Div. 3) B. Substrings Sort 题目连接: http://codeforces.com/contest/988/problem/B Description You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for e

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/