原题链接:http://codeforces.com/problemset/problem/271/D

题目原文:

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.

Examples
input

Copy
ababab
01000000000000000000000000
1
output
5
input

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

题意:

这种题目没什么好讲的,就是让你从一个字符串 s 找出有多少个不同字串并且字串中包含的所有字符是坏的总数不超过k;

解题方法:

NO.1   hash+map

我开始就想用map去solve它,不过当时没想到map的复杂度那么大,就直接把字串放map里面比较,所以就超时了。今天加个hash就可以了,不过时间复杂度还是很大。不建议使用。

unordered_map  无序map,比map时间复杂度低效果差不多就是这样子。不过用法都是一样的。

 1 #include <iostream>
2 #include <cstdio>
3 #include <queue>
4 #include <algorithm>
5 #include <stack>
6 #include <cmath>
7 #include <map>
8 #include <cstring>
9 #include <bits/stdc++.h>
10 using namespace std;
11 #define mod 1000000007
12 #define PI acos(-1.0)
13 typedef long long ll;
14 //const int INF=0x3f3f3f3f;
15 //const ll INF=1000000000000000000;
16 //priority_queue<int, vector<int>, greater<int> >p;
17 int main()
18 {
19 unordered_map<ll,int>m;
20 char s[2000];
21 char a[30];
22 ll k,sum=0;
23 scanf("%s",s);
24 scanf("%s",a);
25 scanf("%lld",&k);
26 int b[2000]={0};
27 int l=strlen(s);
28 for(int i=1;i<=l;i++)
29 b[i]=b[i-1]+((a[s[i-1]-'a']-'0'+1)%2);
30 for(int i=1;i<=l;i++)
31 {
32 ll temp=0;
33 for(int j=i;j<=l;j++)
34 {
35 if(b[j]-b[i-1]<=k)
36 {
37 temp=temp*mod+s[j-1];
38 if(m.find(temp)==m.end())
39 {
40 sum++;
41 m[temp]=1;
42 }
43 }
44 else break;
45 }
46 }
47 printf("%lld\n",sum);
48 return 0;
49 }

NO.2 hash+数组

把字串hash的数值放到数组里面,然后先排序,用unique查重。时间复杂度低了很多了,内存也好了一半。

 1 #include <iostream>
2 #include <cstdio>
3 #include <queue>
4 #include <algorithm>
5 #include <stack>
6 #include <cmath>
7 #include <map>
8 #include <cstring>
9 using namespace std;
10 #define mod 1000000007
11 #define PI acos(-1.0)
12 typedef long long ll;
13 //const int INF=0x3f3f3f3f;
14 //const ll INF=1000000000000000000;
15 //priority_queue<int, vector<int>, greater<int> >p;
16 ll ha[2250005];
17 int main()
18 {
19 map<string,int>m;
20 char s[2000];
21 char a[30];
22 ll k,sum=0;
23 scanf("%s",s);
24 scanf("%s",a);
25 scanf("%lld",&k);
26 int b[2000]={0};
27 int l=strlen(s);
28 for(int i=1;i<=l;i++)
29 b[i]=b[i-1]+((a[s[i-1]-'a']-'0'+1)%2);
30 ll next=0;
31 for(int i=1;i<=l;i++)
32 {
33 ll temp=0;
34 for(int j=i;j<=l;j++)
35 {
36 if(b[j]-b[i-1]>k)break;
37 temp=temp*mod+s[j-1];
38 ha[next++]=temp;
39 }
40 }
41 sort(ha,ha+next);
42 sum=unique(ha,ha+next)-ha;
43 printf("%lld\n",sum);
44 return 0;
45 }

NO.3 hash+set

set直接把重复的数去掉了。。。

 1 #include <iostream>
2 #include <cstdio>
3 #include <queue>
4 #include <algorithm>
5 #include <stack>
6 #include <cmath>
7 #include <map>
8 #include <cstring>
9 #include <bits/stdc++.h>
10 using namespace std;
11 #define mod 1000000007
12 #define PI acos(-1.0)
13 typedef long long ll;
14 //const int INF=0x3f3f3f3f;
15 //const ll INF=1000000000000000000;
16 int main()
17 {
18 set<ll>m;
19 char s[2000];
20 char a[30];
21 ll k,sum=0;
22 scanf("%s",s);
23 scanf("%s",a);
24 scanf("%lld",&k);
25 int b[2000]={0};
26 int l=strlen(s);
27 for(int i=1;i<=l;i++)
28 b[i]=b[i-1]+((a[s[i-1]-'a']-'0'+1)%2);
29 for(int i=1;i<=l;i++)
30 {
31 ll temp=0;
32 for(int j=i;j<=l;j++)
33 {
34 if(b[j]-b[i-1]<=k)
35 {
36 temp=temp*mod+s[j-1];
37 m.insert(temp);
38 }
39 else break;
40 }
41 }
42 sum=m.size();
43 printf("%lld\n",sum);
44 return 0;
45 }

NO.4 hash+字典树

懒得写了,实现起来原理都是一样的。

好了,这个题就讲到这里,我去看下哪个队需要喊666的队友。

codeforces #271D Good Substrings的更多相关文章

  1. Codeforces 271D - Good Substrings [字典树]

    传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  2. Good Substrings CodeForces - 271D

    You've got string s, consisting of small English letters. Some of the English letters are good, the ...

  3. 【CodeForces 271D】Good Substrings

    [链接] 我是链接,点我呀:) [题意] [题解] 字典树 我们可以两重循环(i,j) 来枚举所有的子串 即i=1,j=1,2,3... i=2,j = 2,3,4,.. 于是我们在i变化的时候(就是 ...

  4. Codeforces 316G3 Good Substrings 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...

  5. CodeForces 550A Two Substrings(模拟)

    [题目链接]click here~~  [题目大意]:  You are given string s. Your task is to determine if the given string s ...

  6. Codeforces 1276F - Asterisk Substrings(SAM+线段树合并+虚树)

    Codeforces 题面传送门 & 洛谷题面传送门 SAM hot tea %%%%%%% 首先我们显然可以将所有能够得到的字符串分成六类:\(\varnothing,\text{*},s, ...

  7. Codeforces.392E.Deleting Substrings(区间DP)

    题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...

  8. CodeForces 1110H. Modest Substrings

    题目简述:给定$1 \leq l \leq r \leq 10^{800}$,求一个长度为$n \leq 2000$的数字串$s$,其含有最多的[好]子串.一个串$s$是[好]的,如果将其看做数字时无 ...

  9. @codeforces - 1276F@ Asterisk Substrings

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个包含 n 个小写字母的字符串 s,用 s 生成 n 个串 ...

随机推荐

  1. maven-shade-plugin插件未生效原因分析

    今天在项目的pom文件中引入maven-shade-plugin插件,构建一个uber-jar(包含所有依赖的jar包),但是诡异的事情出现了,执行mvn package后生成的jar包竟然没有包含被 ...

  2. maximo----对比竞品的优势,以及sp的优势

    众多资产密集型企业对eam产品关注度都很高,尤其是eam产品的功能差别,这与行业差别有直接关系,如电力行业.煤炭行业或石油行业等,各行有各行的运营特点,那么eam产品在共性的基础上定出存在细小差异.下 ...

  3. leetcode1546题解【前缀和+贪心】

    leetcode1546.和为目标值的最大数目不重叠非空子数组数目 题目链接 算法 前缀和+贪心 时间复杂度O(n). 1.对nums数组求前缀和: 2.在求前缀和过程中将前缀和sum插入到set集合 ...

  4. 容器云平台No.3~kubernetes使用

    今天是是第三篇,接着上一篇继续 首先,通过kubectl可以看到,三个节点都正常运行 [root@k8s-master001 ~]# kubectl get no NAME STATUS ROLES ...

  5. 如何使用 C# 中的 ValueTask

    在 C# 中利用 ValueTask 避免从异步方法返回 Task 对象时分配 翻译自 Joydip Kanjilal 2020年7月6日 的文章 <How to use ValueTask i ...

  6. l洛谷 P6030 [SDOI2012]走迷宫 概率与期望+高斯消元

    题目描述 传送门 分析 首先判掉 \(INF\) 的情况 第一种情况就是不能从 \(s\) 走到 \(t\) 第二种情况就是从 \(s\) 出发走到了出度为 \(0\) 的点,这样就再也走不到 \(t ...

  7. MySQL: 2、SQL语言

    一.SQL的简介: 1.SQL的概念: SQL就是结构化查询语言,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统 2.SQL的作用:   - ...

  8. RT Thread的SPI设备驱动框架的使用以及内部机制分析

    注释:这是19年初的博客,写得很一般,理解不到位也不全面.19年末得空时又重新看了RTThread的SPI和GPIO,这次理解得比较深刻.有时间时再整理上传. -------------------- ...

  9. 64位系统 system32 和 syswow64

    \Windows\SysWOW64  文件夹下存放32位的库和应用程序 (WOW64 == Windows on Windows 64 bit ) \Windows\System32  文件夹下存放6 ...

  10. Java知识系统回顾整理01基础03变量04类型转换

    一.不同类型之间的数据可以互相转换,但是要满足一定的规则 二.数据类型转换规则 转换规则如图所示  精度高的数据类型就像容量大的杯子,可以放更大的数据 精度低的数据类型就像容量小的杯子,只能放更小的数 ...