Submission #45793179


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/string>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()
using namespace std;
using ll = long long;
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

int main(){
    cin.tie(nullptr); ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> len(n);
    string con = "";
    rep(i,n){
        string s; cin >> s;
        con += s + '?';
        len[i] = s.size();
    }
    auto sa = atcoder::suffix_array(con);
    auto lcp = atcoder::lcp_array(con,sa);
    int siz = con.size();
    vector<int> inv(siz);
    rep(i,siz) inv[sa[i]] = i;
    vector<int> a(siz), b(siz);
    for (int id = 0, i = 0; i < n; i++){
        for (int j = 0; j < len[i]+1; j++, id++){
            a[inv[id]] = i;
            b[inv[id]] = len[i] - j;
        }
    }
    rep(i,siz-1){
        chmin(lcp[i],min(b[i],b[i+1]));
    }
    ll ans = 0;
    for (int l = 0, r = 0; l < siz; l = r){
        while (r < siz && a[r] == a[l]) r++;
        vector<int> sub(r-l+1,0);
        rep(i,r-l+1){
            if (0 <= l-1+i && l-1+i < siz-1) sub[i] = lcp[l-1+i];
        }
        vector<int> lui = sub, rui = sub;
        rep(i,r-l) chmin(lui[i+1],lui[i]);
        reb(i,r-l) chmin(rui[i],rui[i+1]);
        rep(i,r-l){
            int ng = max(lui[i],rui[i+1]);
            if (i > 0) chmax(ng,lcp[l-1+i]);
            ans += max(b[l+i]-ng,0);
        }
    }
    cout << ans << endl;
}

Submission Info

Submission Time
Task H - 404 Chotto Found
User noya2
Language C++ 20 (gcc 12.2)
Score 100
Code Size 1721 Byte
Status AC
Exec Time 178 ms
Memory 31656 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 57
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All handmade-01.txt, handmade-02.txt, handmade-03.txt, handmade-04.txt, random-00.txt, random-01.txt, random-02.txt, random-03.txt, random-04.txt, random-05.txt, random-06.txt, random-07.txt, random-08.txt, random-09.txt, random-10.txt, random-11.txt, random-12.txt, random-13.txt, random-14.txt, random-15.txt, random-16.txt, random-17.txt, random-18.txt, random-19.txt, random-20.txt, random-21.txt, random-22.txt, random-23.txt, random-24.txt, random-25.txt, random-26.txt, random-27.txt, random-28.txt, random-29.txt, random-30.txt, random-31.txt, random-32.txt, random-33.txt, random-34.txt, random-35.txt, random-36.txt, random-37.txt, random-38.txt, random-39.txt, random-40.txt, random-41.txt, random-42.txt, random-43.txt, random-44.txt, random-45.txt, random-46.txt, random-47.txt, random-48.txt, random-49.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
handmade-01.txt AC 1 ms 3640 KiB
handmade-02.txt AC 178 ms 30860 KiB
handmade-03.txt AC 93 ms 28316 KiB
handmade-04.txt AC 29 ms 9284 KiB
random-00.txt AC 143 ms 31656 KiB
random-01.txt AC 146 ms 31612 KiB
random-02.txt AC 144 ms 31652 KiB
random-03.txt AC 150 ms 26772 KiB
random-04.txt AC 147 ms 26736 KiB
random-05.txt AC 149 ms 26732 KiB
random-06.txt AC 144 ms 31608 KiB
random-07.txt AC 143 ms 27640 KiB
random-08.txt AC 144 ms 23756 KiB
random-09.txt AC 140 ms 31624 KiB
random-10.txt AC 141 ms 25780 KiB
random-11.txt AC 141 ms 25840 KiB
random-12.txt AC 153 ms 25836 KiB
random-13.txt AC 136 ms 25868 KiB
random-14.txt AC 151 ms 25704 KiB
random-15.txt AC 150 ms 25904 KiB
random-16.txt AC 142 ms 25808 KiB
random-17.txt AC 151 ms 25840 KiB
random-18.txt AC 143 ms 25896 KiB
random-19.txt AC 138 ms 25788 KiB
random-20.txt AC 153 ms 28276 KiB
random-21.txt AC 133 ms 28064 KiB
random-22.txt AC 151 ms 28248 KiB
random-23.txt AC 125 ms 27916 KiB
random-24.txt AC 152 ms 28056 KiB
random-25.txt AC 147 ms 28080 KiB
random-26.txt AC 154 ms 27940 KiB
random-27.txt AC 147 ms 28096 KiB
random-28.txt AC 153 ms 28032 KiB
random-29.txt AC 152 ms 28012 KiB
random-30.txt AC 162 ms 27396 KiB
random-31.txt AC 163 ms 27408 KiB
random-32.txt AC 163 ms 27364 KiB
random-33.txt AC 162 ms 27348 KiB
random-34.txt AC 161 ms 27400 KiB
random-35.txt AC 164 ms 27328 KiB
random-36.txt AC 163 ms 27428 KiB
random-37.txt AC 161 ms 27324 KiB
random-38.txt AC 162 ms 27312 KiB
random-39.txt AC 162 ms 27356 KiB
random-40.txt AC 164 ms 28696 KiB
random-41.txt AC 165 ms 28612 KiB
random-42.txt AC 164 ms 28656 KiB
random-43.txt AC 164 ms 28648 KiB
random-44.txt AC 163 ms 28572 KiB
random-45.txt AC 165 ms 28804 KiB
random-46.txt AC 166 ms 28648 KiB
random-47.txt AC 164 ms 28596 KiB
random-48.txt AC 163 ms 28832 KiB
random-49.txt AC 165 ms 28504 KiB
sample-01.txt AC 1 ms 3384 KiB
sample-02.txt AC 1 ms 3588 KiB
sample-03.txt AC 1 ms 3520 KiB