码迷,mamicode.com
首页 > 其他好文 > 详细

【CodeForces 271D】Good Substrings

时间:2018-10-03 23:27:46      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:long   cin   pen   ifd   规则   c++   problems   i++   题意   

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

【题解】


字典树
我们可以两重循环(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

标签:long   cin   pen   ifd   规则   c++   problems   i++   题意   

原文地址:https://www.cnblogs.com/AWCXV/p/9739070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!