传送门

D. Good Substrings
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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

9813017 2015-02-13 06:13:52 njczy2010 271D - Good Substrings GNU C++ Accepted 248 ms 53000 KB 
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 2250005
#define M 1505
#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int T;
int flag;
int ccount;
char ss[M];
char f[]; typedef struct
{
int v;
vector<int>nt;
}PP; PP p[N];
int le; int k;
int tot; void insert(char s[])
{
int l=strlen(s);
int i;
int ff;
int st=;
int y;
vector<int>::iterator it;
for(i=;i<l;i++){
ff=;
for(it=p[st].nt.begin();it!=p[st].nt.end();it++){
y=*it;
if(p[y].v==s[i]-'a'){
ff=;
st=y;
break;
}
}
if(ff==){
p[st].nt.push_back(tot);
p[tot].v=s[i]-'a';
p[tot].nt.clear();
st=tot;
tot++;
}
}
} void ini()
{
ccount=;
int i;
scanf("%s",f);
scanf("%d",&k);
flag=;
p[].nt.clear();
p[].v=-;
tot=;
scanf("%d",&n);
le=strlen(ss);
for(i=;i<le;i++){
insert(ss+i);
}
} void check(int st,int now)
{
//printf(" st=%d v=%d now=%d\n",st,p[st].v,now);
int y;
if(now>k) return;
ccount++;
vector<int>::iterator it;
for(it=p[st].nt.begin();it!=p[st].nt.end();it++)
{
y=*it;
//printf(" st=%d y=%d f=%c\n",st,y,f[ p[y].v ]);
if(f[ p[y].v ]==''){
check(y,now);
}
else{
check(y,now+);
}
}
} void solve()
{
//printf("solve\n");
ccount=-;
check(,);
} void out()
{
printf("%d\n",ccount);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%s",ss)!=EOF)
{
ini();
solve();
out();
}
return ;
}

Codeforces 271D - Good Substrings [字典树]的更多相关文章

  1. codeforces #271D Good Substrings

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

  2. Codeforces 665E. Beautiful Subarrays (字典树)

    题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...

  3. Choosing The Commander CodeForces - 817E (01字典树+思维)

    As you might remember from the previous round, Vova is currently playing a strategic game known as R ...

  4. Codeforces 948D Perfect Security(字典树)

    题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...

  5. Codeforces 282E Sausage Maximization(字典树)

    题目链接:282E Sausage Maximization 题目大意:给定一个序列A.要求从中选取一个前缀,一个后缀,能够为空,当时不能重叠.亦或和最大. 解题思路:预处理出前缀后缀亦或和,然后在字 ...

  6. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  7. Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)

    E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...

  8. codeforces 706D (字典树)

    题目链接:http://codeforces.com/problemset/problem/706/D 题意:q次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...

  9. 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset

    题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...

随机推荐

  1. leetcode_1052. Grumpy Bookstore Owner

    1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...

  2. 用dfs求联通块(UVa572)

    一.题目 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符所在的格子相邻(横.竖.或者对角线方向),就说它们属于同一个八连块. 二.解题思路 和前面的二叉树遍历类似,图也有DF ...

  3. C#中Lock关键字的使用

    C# 中的 Lock 语句通过隐式使用 Monitor 来提供同步功能.lock 关键字在块的开始处调用 Enter,而在块的结尾处调用 Exit. 通常,应避免锁定 public 类型,否则实例将超 ...

  4. JAVA中IP和整数相互转化(含有掩码的计算)

    import java.net.InetAddress;/** * 用于IP和整数之间的相互转换 * @author Andy.Wang * */public class IPv4Util {    ...

  5. Java InputStream、String、File相互转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  6. 1008: ASCII码

    题目描述 相信大家一定都知道大名鼎鼎的ASCII码,这次给你的任务是输入数字(表示ASCII码),输出相对应的字符信息. 输入 第一行为一个整数T(1<=T<=1000).接下来包括T个正 ...

  7. Android Studio问题记录

    1>Android Studio中module是什么,? 答:Android Studio是基于intellij,跟eclipse不太一样.对应关系如下: intellij的project -- ...

  8. GIMP选择区域Selection Editor

    如图我要选择该图的衣服部分和这个球的部分, 选择Select下的Selection Editor工具,然后点击魔法棒工具(Fuzzy Select Tool),选择衣服: 需要注意以下白色部分是选择的 ...

  9. perl中foreach(一)

    perl中的foreach结构  首先语法 foreach $rock(qw /bedrock slate lava/){        $rock="\t$rock";      ...

  10. python的部分内置函数

    内置函数思维导图:https://www.processon.com/mindmap/5c10ca52e4b0c2ee256ac034 内置函数 匿名函数 匿名函数统一的名字是:<lambda& ...