【链接】 我是链接,点我呀:)

【题意】

【题解】

字典树
我们可以两重循环(i,j)
来枚举所有的子串
即i=1,j=1,2,3...
i=2,j = 2,3,4,..
于是我们在i变化的时候(就是j层循环完了,i要执行i+1的时候
令cur=字典树的root
然后沿着字典树往下走。
遇到没有走过的位置,就说明我们找到了一个之前没有碰到的子串。
那么我们就++tot,按照字典树的规则,创建一个新的节点。
否则如果有这个节点,那么就接着往下走就好,说明这个子串s[i..j]之前出现过
然后如果特殊字母>=k了,那么就直接break即可。
最后字典树的节点个数就是符合要求的子串个数.

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++) using namespace std; const int N = 1500*1500; string s;
string dic;
int ch[N+10][26];
int k,tot,root = 1; int main()
{
#ifdef LOCAL_DEFINE
freopen("rush.txt","r",stdin);
#endif // LOCAL_DEFINE
ios::sync_with_stdio(0),cin.tie(0);
cin >> s;
cin >> dic;
cin >> k;
tot = 1;
rep1(i,0,(int)s.size()-1){
int cur = root;
int cnt = 0;
rep1(j,i,(int)s.size()-1){
if (dic[s[j]-'a']=='0') cnt++;
if (cnt>k) break;
if (ch[cur][s[j]-'a']==0)ch[cur][s[j]-'a']=++tot;
cur = ch[cur][s[j]-'a'];
}
}
cout<<tot-1<<endl;
return 0;
}

【CodeForces 271D】Good Substrings的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【Codeforces 258D】 Count Good Substrings

    [题目链接] http://codeforces.com/contest/451/problem/D [算法] 合并后的字符串一定是形如"ababa","babab&qu ...

  3. 【19.46%】【codeforces 551B】ZgukistringZ

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 766C】Mahmoud and a Message

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. 进行mysql压力測试须要注意的几点

    1.填充測试数据比物理内存还要大,至少超过innodb_buffer_pool_size 值,不能将数据所有装载到内存中,除非你的本意就想測试全内存状态下的MySQL性能. 2.每轮測试完毕后,都重新 ...

  2. Spark Streaming源代码学习总结(一)

    1.Spark Streaming 代码分析: 1.1 演示样例代码DEMO: 实时计算的WorldCount: import org.apache.spark.streaming.{Seconds, ...

  3. sql处理高并发

    sql处理高并发,防止库存超卖 2014-08-14 23:44 13560人阅读 评论(2) 收藏 举报  分类: 数据库(43)  今天王总又给我们上了一课,其实mysql处理高并发,防止库存超卖 ...

  4. Springboot 之 引入Thymeleaf

    转自:https://segmentfault.com/a/1190000011149325 前言 Spring-boot-starter-web集成了Tomcat以及Spring MVC,会自动配置 ...

  5. Dijkstra算法原理及证明(转)

    Dijkstra算法及其证明 算法: 设G是带权图,图中的顶点多于一个,且所有的权都为正数.本算法确定从顶点S到G中其他各个顶点的距离和最短通路.在本算法中P表示带永久标记的顶点的集合.顶点A的前驱是 ...

  6. Comparable与Comparator源码分析

    package java.lang; import java.util.*; /** * This interface imposes a total ordering on the objects ...

  7. Svn install and use

    1.安装服务 使用yum安装subversion,简单.不繁琐. 1 yum install -y subversion 2.创建版本库 1 2       mkidr /svn/obj        ...

  8. C#监测方法执行效率

    System.Diagnostics.Stopwatch watch = new Stopwatch(); watch.Start(); // 开始监视代码运行时间 //需要监测的代码 dothing ...

  9. 【Oracle】使用logmnr挖掘日志

    使用Logmnr挖掘日志的详细步骤如下: 1. 查看日志状态及位置 col member for a45 select group#,status from v$log; select group#, ...

  10. 配置postgreSQL允许外部连接

    配置远 程连接PostgreSQL数据库的步骤很简单,只需要修改 %PostgreSQL_path%/data 目录下的 pg_hba.conf 和 postgresql.conf. 一.修改pg_h ...