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, ..., srthere 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 1500characters.

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个】

  

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
const int maxn = 1e4 + ;
string str1, str2;
int k, a[maxn];
LL seed[]={, , };
int main() {
cin >> str1 >> str2 >> k;
a[] = (str2[str1[] - 'a'] == '');
for (int i = ; i < str1.size() ; i++)
a[i] = a[i - ] + (str2[str1[i] - 'a'] == '');
set<LL>st;
for (int i = ; i < str1.size() ; i++) {
LL HASH = str1[i] - 'a' + ;
for (int j = i ; j < str1.size() ; j++) {
if (i == j && (str2[str1[j] - 'a'] == '') <= k ) st.insert(str1[j] - 'a');
else {
if (a[j] - a[i] + (str2[str1[i] - 'a'] == '') > k) break;
HASH += (HASH * seed[str1[j] % ] + str1[j] - 'a') % mod;
st.insert(HASH);
}
}
}
printf("%d\n", st.size());
return ;
}

Good Substrings CodeForces - 271D的更多相关文章

  1. Codeforces 271D - Good Substrings [字典树]

    传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  2. codeforces #271D Good Substrings

    原题链接:http://codeforces.com/problemset/problem/271/D 题目原文: D. Good Substrings time limit per test 2 s ...

  3. 【CodeForces 271D】Good Substrings

    [链接] 我是链接,点我呀:) [题意] [题解] 字典树 我们可以两重循环(i,j) 来枚举所有的子串 即i=1,j=1,2,3... i=2,j = 2,3,4,.. 于是我们在i变化的时候(就是 ...

  4. Many Equal Substrings CodeForces - 1029A (kmp next数组应用)

    题目大意 题目看样例也能猜到就是输出最短的循环串. 吐槽 明明是div3第一题为啥子还会用到kmp的知识? 解法 这个题仔细看发现是求最长可去除的后缀,也就是说去除跟下一个相同的字符串还能连接起来.这 ...

  5. Erasing Substrings CodeForces - 938F (字符串dp)

    大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字 ...

  6. codeforces271D

    Good Substrings CodeForces - 271D 给你一个只包含小写字母的字符串s.问你在这个字符串中有多少个不同的子串.且要求这些子串中不得出现超过k个的特殊字母.*子串s1和子串 ...

  7. Codeforces Round #306 (Div. 2) A. Two Substrings 水题

    A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

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

    B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题

    D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...

随机推荐

  1. 多线程编程以及socket编程_Linux程序设计4chapter15

    看了Linux程序设计4中文版,学习了多线程编程和socket编程.本文的程序参考自Linux程序设计4的第15章. 设计了一个客户端程序,一个服务端程序.使用TCP协议进行数据传输. 客户端进程创建 ...

  2. R语言学习笔记(五):零碎知识点(11-15)

    11--which.min(), which.max()和which() which(x, arr.ind = FALSE, useNames = TRUE) x 是一个向量或者数组,可以是NA,但会 ...

  3. es同步mysql同步-logstash

    1.下载es https://www.elastic.co/downloads/elasticsearch 修改 config 下elasticsearch.yml   ip和端口等配置 2.下载ki ...

  4. C# List集合去重操作注意点

    今天调试代码时发现list的distinct方法在对引用类型操作时并没有去重,后来查阅资料发现list去重操作对象集合时比较的是对象的一个个引用地址, 因为集合里的对象都是一个个单独的实例,所以并不会 ...

  5. model的index无限次数执行导致stackOverFlow

    model的index无限次数执行导致stackOverFlow

  6. [转]Git,SVN的优缺点及适合的范围,开源项目?公司项目?

    使用git不久,粗浅理解: 1)适用对象不同.Git适用于参与开源项目的开发者.他们由于水平高,更在乎的是效率而不是易用性.Svn则不同,它适合普通的公司开发团队.使用起来更加容易. 2)使用的场合不 ...

  7. .netcore centos环境搭建实战

    步骤 1. 安装VMware Workstation 下载地址:https://my.vmware.com/cn/web/vmware/info/slug/desktop_end_user_compu ...

  8. Bugku 速度要快

    import requests import base64 url="http://123.206.87.240:8002/web6/" res=requests.get(url) ...

  9. 做小Leader的心得体会

    只是自己的工作心得体会,代码属于也不够专业,大家不喜勿喷. 8月份来到这家新公司,没过一个月给派了个活:带着两个小弟给某银行开发一个小工具.功能很简单,就是用Java做一个windows上的C端工具, ...

  10. Ubuntu16.04安装Zabbix

    基于Zabbix+MySQL+Apache(可选) apt-get install php7.0-bcmath php7.0-xml php7.0-mbstring安装Zabbix所需的几个PHP模块 ...