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. Go web表单

    package main import ( "fmt" "html/template" "log" "net/http" ...

  2. Redis 在springBoot中的一个使用示例

    在现系统中使用了一个字典表,更新或插入字典表需要做Redis缓存 @Override @Cache(name = Constants.REDIS_PREFIX_DIC, desc = "变更 ...

  3. SKIP(插入空行)

    WRITE 'This is the 1st line'. SKIP. WRITE 'This is the 2nd line'. 跳转至某一行 SKIP TO LINE line_number. 插 ...

  4. 20145202马超 2006-2007-2 《Java程序设计》第2周学习总结

    20145202马超 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要讲了各种变量的设置以及流程控制,基本上都和c是一样的.print是不太一样的, ...

  5. PHP HashTable总结

    本篇文章主要是对 PHP HashTable 总结,下面的参考链接是很好的学习资料. 总结 HashTable 又叫做散列表,是一种用于以常数平均时间执行插入.删除和查找的技术.不能有效的支持元素之间 ...

  6. centos下搭建svn服务器端/客户端

    1.安装 yum install subversion httpd mod_dav_svn 2.创建仓库存储代码 mkdir /var/repos svnadmin create /var/repos ...

  7. 单服务器最大tcp连接数及调优汇总

    启动线程数: 启动线程数=[任务执行时间/(任务执行时间-IO等待时间)]*CPU内核数 最佳启动线程数和CPU内核数量成正比,和IO阻塞时间成反比.如果任务都是CPU计算型任务,那么线程数最多不超过 ...

  8. android去掉button默认的点击阴影

    查了资料,发现别人都是说加一个style属性. style="?android:attr/borderlessButtonStyle" 加上了确实管用,但是我绝不是不求甚解的人.追 ...

  9. ORB-SLAM(五)KeyFrame类

    KeyFrame类利用Frame类来构造.对于什么样的Frame可以认为是关键帧以及何时需要加入关键帧,是实现在tracking模块中的. 由于KeyFrame中一部分数据会被多个线程访问修改,因此需 ...

  10. TortoiseHg 学习笔记一

    因项目需要频繁的提交.合并代码,前段时间别人在我笔记本上装了个TortoiseHg,我不怎么会用 现在学习一下,目前没时间写太多东西,未完待续 记录一下查看的文章 http://jingyan.bai ...