传送门

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. hdu 6058 Kanade's sum (计算贡献,思维)

    题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...

  2. 文本编辑器vim/vi用法完全解读

    vi用法 1.启动vim 2.命令模式和输入模式 3.退出vi 4.vi与ex命令 5.移动光标 6.跳转 7.搜索 8.插入文本 9.修改文本 10.替换文本 11.删除文本 12.恢复和撤销改变 ...

  3. java并发编程:Executor、Executors、ExecutorService

    1.Executor和ExecutorService Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable comma ...

  4. delphi win7 and high path

    Close DelphiLocate bordbk120N.dll (C:\Program Files (x86)\CodeGear\RAD Studio\6.0\bin)Make a backup ...

  5. vue引用文件

    1)css引入在vue页面中<style scoped> @import url("../assets/css/home.css");</style>2)j ...

  6. percona-server-5.7二进制安装(tokudb)

    1.下载二进制安装包(适用于红帽.centos) https://www.percona.com/downloads/Percona-Server-LATEST/Percona-Server-5.7. ...

  7. (8)zabbix监控项item是什么

    什么是item Items是从主机里面获取的所有数据.通常情况下我叫itme为监控项,例如服务器加入了zabbix监控,我需要监控它的cpu负载,那么实现这个方法的东西就叫item item构成 it ...

  8. 如何把握好 transition 和 animation 的时序,创作描边按钮特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/mKdzZM 可交互视频教 ...

  9. django第四天(路由别名,django2.x新特性和自定义转换器)

    django第四天 路由别名 1.路由别名: 给路由路径命名一个名字 url(r'^login/$',views.login,name = 'login') 2.为什么要用路由别名 ①当路由路径过长时 ...

  10. uboot的readme

    ## (C) Copyright 2000 - 2008# Wolfgang Denk, DENX Software Engineering, wd@denx.de.## See file CREDI ...