【CodeForces 271D】Good Substrings

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.