【CodeForces 271D】Good Substrings
【链接】 我是链接,点我呀:)
【题意】
【题解】
字典树
我们可以两重循环(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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【Codeforces 258D】 Count Good Substrings
[题目链接] http://codeforces.com/contest/451/problem/D [算法] 合并后的字符串一定是形如"ababa","babab&qu ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766C】Mahmoud and a Message
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- iOS GCD使用指南
Grand Central Dispatch(GCD)是异步运行任务的技术之中的一个. 一般将应用程序中记述的线程管理用的代码在系统级中实现.开发人员仅仅须要定义想运行的任务并追加到适当的Dispat ...
- Spring JDBC数据库开发
针对数据库操作,Spring框架提供了JdbcTemplate类. 1.Spring JDBC的配置 创建配置文件applicationContext.xml,添加如下代码: <!--配置数据源 ...
- 2016.04.19,英语,《Vocabulary Builder》Unit 16
top, comes from topos, the Greek word for 'place'. ectopic: [ek'tɑːpɪk] adj. [医]异位的,异常的 topical: ['t ...
- oc16--set,get
// // Kline.h // day13 #import <Foundation/Foundation.h> @interface Kline : NSObject { int _ma ...
- Windows 10彻底关闭自动更新
关键点:把流量计费开启.
- 0x58 数据结构优化DP
补写一下 poj3171 设f[i]表示覆盖L~i的最小花费,把区间按左端点排序,枚举区间,f[a[i].r]=min{f[a[i].l~(a[top].r-1)]}+a[i].c (当然还要和原值比 ...
- ps -aux ,ps aux ,ps -ef 的区别
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- 推荐使用sublime text 3 以及常用快捷键
vim这种上古神器,需要学习.记忆.折腾.比如我的初衷是要开发php的,连php都没专研透,哪有精力去折腾vim这玩意. 当然,vim绝技练成以后,配置成各种IDE都不是问题,还有你手速会飞起来. 但 ...
- JavaScript的并且&&
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- Object的wait和Thread的sleep
Object的wait() wait()搭配notify(),nofityAll()使用. 线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notif ...