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  =  s_1s_2...s_{|}s| (where |s| is the length of string s) is string  s_{l}s_{l} + 1...s_{r}.

The substring s[l...r] is good, if among the letters  s_{l}, s_{l} + 1, ..., s_{r} 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".

Difficulty: interview

Starter code:

Solutions:
s = input()
L = input()
k = int(input())

'''from datetime import *
time1 = datetime.now()'''
good = set()
string = set()
LIST = [chr(i) for i in range(97,123)]
for i in range(26):
    if L[i]=='1':
        good.add(LIST[i])

t = [s[i] not in good for i in range(len(s))]

end = [0]*len(s)
badchars = 0
front=0; rear=0
while(front<len(s)):
    while(rear<len(s)):
        badchars+=t[rear]
        if badchars>k:
            badchars-=1
            break
        rear+=1
    end[front]=rear
    badchars -= t[front]
    front+=1

for i in range(len(s)):
    tempStrHash = 0
    for j in range(i, end[i]):
        tempStrHash = (tempStrHash*29+ord(s[j])-96)&1152921504606846975
        string.add(tempStrHash)
        
print(len(string))
#print(datetime.now()-time1)


s = input()
a = input()
k = int(input())
S=sorted(s[i:] for i in range(len(s)))
p=''
r=0
for e in S:
    t=0
    s=0
    for i in range(len(e)):
        if i >= len(p) or e[i] != p[i]: s=1
        t+=a[ord(e[i])-ord('a')]=='0'
        if t > k:break
        if s: r+=1
    p = e
print(r)

s = input()
a = input()
k = int(input())
S=sorted(s[i:] for i in range(len(s)))
p=''
r=0
for e in S:
    t=0
    s=0
    for i in range(len(e)):
        if i >= len(p) or e[i] != p[i]: 
          s=1
        if a[ord(e[i])-ord('a')]=='0':
          t+=1
        if t > k:
          break
        if s: 
          r+=1
    p = e
print(r)

s = input()
L = input()
k = int(input())

good = set()
string = set()
LIST = [chr(i) for i in range(97, 123)]
for i in range(26):
    if L[i] == '1':
        good.add(LIST[i])

t = [s[i] not in good for i in range(len(s))]

end = [0]*len(s)
badchars = 0
front = 0
rear = 0
while(front < len(s)):
    while(rear < len(s)):
        badchars += t[rear]
        if badchars > k:
            badchars -= 1
            break
        rear += 1
    end[front] = rear
    badchars -= t[front]
    front += 1

for i in range(len(s)):
    tempStrHash = 0
    for j in range(i, end[i]):
        tempStrHash = (tempStrHash*29+ord(s[j])-96) & 1152921504606846975
        string.add(tempStrHash)

print(len(string))


s = input()
l = input()
k = int(input())
no_bad = []
curr = 0
p  =31
m = 67280421310721
hashes = []
currh = 0
cnt = 0
P = [pow(p,i,m) for i in range(len(s)+2)]
for i in s:
    currh+=(P[cnt]*ord(i))%m
    cnt+=1
    hashes.append(currh)
    if(l[ord(i) - ord('a')] =='0'):
        no_bad.append(curr+1)
        curr+=1
    else:
        no_bad.append(curr)
no_bad.append(0)
hashes.append(0)
ans = set()
for i in range(len(s)):
    for j in range(i,len(s)):
        if(no_bad[j] - no_bad[i-1]<=k):
            has = ((hashes[j] - hashes[i-1])*P[len(s)-i+1])%m
            ans.add(has)
print(len(ans))

# -*- coding:utf-8 -*-

"""

created by shuangquan.huang at 1/7/20

"""

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List


def solve(S, B, K):
    MOD = (1 << 50) + 9
    s = set()
    
    N = len(S)
    pow = [1 for _ in range(N+1)]
    for i in range(1, N+1):
        pow[i] = pow[i-1] * 26
        pow[i] %= MOD
    
    for i in range(N-1, -1, -1):
        k, h = 0, 0
        for j in range(i, -1, -1):
            ch = S[j]
            if B[ch]:
                k += 1
            if k > K:
                # print(i, j, S[i: j+1])
                break
            h += (ord(ch) - ord('a') + 1) * pow[i-j]
            h %= MOD
            # print(S[i: j + 1], h)
            s.add(h)
    return len(s)
    
    
S = input()
B = list(input())
K = int(input())

B = {chr(ord('a') + i): False if B[i] == '1' else True for i in range(26)}
print(solve(S, B, K))



s = input()
a = input()
k = int(input())
S=sorted(s[i:] for i in range(len(s)))
p=''
r=0
for e in S:
    t=0
    s=0
    for i in range(len(e)):
        if i >= len(p) or e[i] != p[i]: s=1
        t+=a[ord(e[i])-ord('a')]=='0'
        if t > k:break
        if s: r+=1
    p = e
print(r)

s = input()
L = input()
k = int(input())
 
'''from datetime import *
time1 = datetime.now()'''
good = set()
string = set()
LIST = [chr(i) for i in range(97,123)]
for i in range(26):
    if L[i]=='1':
        good.add(LIST[i])
 
t = [s[i] not in good for i in range(len(s))]
 
end = [0]*len(s)
badchars = 0
front=0; rear=0
while(front<len(s)):
    while(rear<len(s)):
        badchars+=t[rear]
        if badchars>k:
            badchars-=1
            break
        rear+=1
    end[front]=rear
    badchars -= t[front]
    front+=1
 
for i in range(len(s)):
    tempStrHash = 0
    for j in range(i, end[i]):
        tempStrHash = (tempStrHash*29+ord(s[j])-96)&1152921504606846975
        string.add(tempStrHash)
        
print(len(string))


s = input()
a = input()
k = int(input())
d = sorted(s[i:] for i in range(len(s)))

c = ''
r = 0

for j in d:
	pos = 0
	s = 0
	for i in range(len(j)):
		if i >= len(c) or j[i] != c[i]:
			s = 1
		if a[ord(j[i]) - ord('a')] == '0':
			pos += 1
		if pos > k:
			break
		if s:
			r += 1
	c = j
print(r)

import sys
import math as mt
#input=sys.stdin.buffer.readline
#t=int(input())
t=1
mod=9007199254740881
for ___ in range(t):
    #n,m=map(int,input().split())
    s1=input()
    s2=input()
    k=int(input())
    d={}
    
    for i in range(len(s1)):
        suma=0
        pre=0
        D=256
        for j in range(i,len(s1)):
            pre=(pre*D+ord(s1[j]))%mod
            posi=ord(s1[j])-ord('a')
            if s2[posi]=='0':
                suma+=1
            if suma<=k:
                d[pre]=1
            else:
                break
    print(len(d))        

s = input()
L = input()
k = int(input())
l=len(s)
good = set()
string = set()
LIST = [chr(i) for i in range(97, 123)]
for i in range(26):
    if L[i] == '1':
        good.add(LIST[i])
t = [s[i] not in good for i in range(l)]
end = [0]*l
sumbad = 0
i,j=0,0
while i<l:
    if j<l:
        sumbad+=t[j]
    if sumbad>k or j==l:
        sumbad-=t[i]
        end[i]=j
        i+=1
        if sumbad>k:
            sumbad-=t[j]
            continue
    if j<l:
        j+=1
for i in range(len(s)):
    t = 0
    for j in range(i, end[i]):
        t = (t*29 + ord(s[j])-96)&1152921504606846975
        string.add(t)
print(len(string))

s=input()
good=[int(x) for x in input()]
k=int(input())
n=len(s)
ans=0
mod=10**15+9
dp=[[0 for _ in range(n)] for _ in range(n)]
hashes=[(ord(s[0])-ord('a')+1)]
if good[ord(s[0])-ord('a')]:
    dp[0][0]=0
else:
    dp[0][0]= 1
for i in range(1,n):
    hashes.append(hashes[-1]+(ord(s[i])-ord('a')+1)*(29**i))
for i in range(n):
    hashes[i]=hashes[i]%(mod)
occ=set()
ppowi=[]
for i in range(n+1):
    ppowi.append((29**i)%mod)
for i in range(n):
    for j in range(i,n):
        if i-1>=0:x=((hashes[j]-hashes[i-1])*(ppowi[n-i]))%mod
        else:x=((hashes[j])*(ppowi[n]))%mod
        if i==0 and j==0:
            dp[i][j]=1^good[ord(s[j])-ord('a')]
        elif good[ord(s[j])-ord('a')]==0:
            dp[i][j]=dp[i][j-1]+1
        else:
            dp[i][j]=dp[i][j-1]
        if dp[i][j]>k:break
        if dp[i][j]<=k:
            occ.add(x)
print(len(occ))

import collections as cc
import sys
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
S=lambda :list(input().strip())
s=S()
t=S()
k,=I()
ans=0
prev=''
ss=sorted([s[i:] for i in range(len(s))])
for j in ss:
	now=0
	f=0
	for i in range(len(j)):
		if i>=len(prev) or j[i]!=prev[i]:
			f=1
		now+=t[ord(j[i])-ord('a')]=='0'
		if now>k:
			break
		if f:
			ans+=1
	prev=j
print(ans)