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

【题意】

【题解】

字典树
我们可以两重循环(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. 原型设计模式prototype-构造js自己定义对象

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 有一种蓝叫 APEC 蓝

    有如是解释 APEC 者--Air Pollution Eventually Controlled. 有说此次是继零八后的重新万国来朝.丝路大略明白了,西域必通. 站在历史的远处回眸,这是继零八年后重 ...

  3. [BZOJ 3387] Fence Obstacle Course

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3387 [算法] f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数 f[i ...

  4. B1968 [Ahoi2005]COMMON 约数研究 数论

    大水题,一分钟就做完了...直接枚举1~n就行了,然后在n中判断出现多少次. 题干: Description Input 只有一行一个整数 N(0 < N < 1000000). Outp ...

  5. java javax.annotation.Resource注解的详解

    转自:https://www.jb51.net/article/95456.htm java 注解:java javax.annotation.Resource  当我们在xml里面为类配置注入对象时 ...

  6. navicat数据库同步

    10.168.95.246

  7. encodeURIComponent编码java后台解码出现乱码问题

    问题:JavaScript请求后台带着name参数,有中文进行编码:url?name=" + encodeURIComponent(name):java后台直接使用name或者name=ja ...

  8. Python 3.x 判断 dict 是否包含某个键

    Python 3.x不再支持 has_key() 函数,而被__contains__('key')所替代,会返回bool,可以用其做判断. 代码示例: >>> user = 'dad ...

  9. mysql 登录与权限

    一.mysql 登录方式 1.1 格式:mysql -u用户名 -p密码 -h ip -P 端口 -S 套接字 mysql -uvagrant -pvagrant -h 127.0.0.1 -P 33 ...

  10. python ansible api

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : test2.py # @Author: Anthony.waa # @Date : 20 ...