传送门

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. (转)SpringMVC学习(五)——SpringMVC的参数绑定

    http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...

  2. myeclipse 导入项目时no projects are found to import解决办法

    myeclipse 识别一个工程需要.classpath与.project文件,一般无需提交SVN所以项目切下来的时候是没有这两个文件的. 方法1: 1) 在myeclipse中新建一个和你要导入的项 ...

  3. idea快速生成实体类Entity

    1)打开idea 2)添加mysql的数据连接 3)生成类

  4. jExcelAPI导入导出excel

      MS的电子表格(Excel)是Office的重要成员,是保存统计数据的一种常用格式.作为办公文档,势必要涉及到的电子文档的交换,Excel是一种在企业中非常通用的文件格式,打印和管理也比较方便.在 ...

  5. C#值类型和引用类型与Equals方法

    1. C#的值类型和引用类型 C#的对象里面有两种类型,一个是引用类型,一个是值类型,值类型和引用类型的具体分类可以看下面的分类.   在C#中,不管是引用类型还是值类型,他们都隐式继承Object类 ...

  6. shell脚本,按单词出现频率降序排序。

    [root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...

  7. React初识整理(三)--受控组件解决方法

    1. 受控组件:组件处于受控制状态,不可更改输入框内的值. 2. 什么情况下会让组件变成受控组件? - 文本框设置了value属性的时候 - 单选框或多选框设置了checked属性的时候. 3. 如何 ...

  8. vue父子传值的具体应用

    最近我负责的项目已经迭代到第四版了,我作为一个没啥经验的小菜鸟也成长了很多. 在这一版开发开始之前,我老大就要求我在开发过程中尽量实现组件化,因此,我也遇到了很多问题,但基本都解决了,所以趁周末把这些 ...

  9. 转载:jquery.ajax之beforeSend方法使用介绍

    常见的一种效果,在用ajax请求时,没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据.这个就可以用beforeSend方法来实现. 下载demo:a ...

  10. python爬虫(爬取视频)

    爬虫爬视频 爬取步骤 第一步:获取视频所在的网页 第二步:F12中找到视频真正所在的链接 第三步:获取链接并转换成机械语言 第四部:保存 保存步骤代码 import re import request ...