"Codeforces 271D" good substrings

Source: Internet
Author: User

"Link" I am the link, point me:)
Test instructions

Exercises


Dictionary Tree
We can double cycle (i,j)
To enumerate all the substrings
That is i=1,j=1,2,3 ...
I=2,j = 2,3,4,..
So we are at the time of I change (that is, the J layer cycle is over, I want to execute i+1
Make the root of the cur= dictionary tree
Then go down the dictionary tree.
When we encounter a position that has not been traversed, we find a substring that we have not encountered before.
Then we'll ++tot to create a new node according to the rules of the dictionary tree.
Otherwise, if there is this node, then go down and walk on it, indicating this substring s[i. J] There have been
Then if the special letter >=k, then the direct break can be.
The number of nodes in the last dictionary tree is the number of substrings that meet the requirements.

Code

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.