답안 #234390

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
234390 2020-05-24T06:52:43 Z VEGAnn Lozinke (COCI17_lozinke) C++14
100 / 100
67 ms 17552 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 20100;
string s[N];
int n, tt = 0;
ll ans = 0;

bool cmp(string _x, string _y){
    return sz(_x) < sz(_y);
}

struct trie{
    trie *next[26];
    int kol;

    trie(): kol(0) {
        for (int i = 0; i < 26; i++)
            next[i] = 0;
    }

    void insert(string &s, int pos){
        if (pos == sz(s)) {
            kol++;
            return;
        }

        int ch = (s[pos] - 'a');

        if (next[ch] == 0)
            next[ch] = new trie();

        next[ch] -> insert(s, pos + 1);
    }
};

trie *root;

unordered_map<trie*, int> mem;

void check(trie* cur, string &s, int pos){
    if (pos == sz(s)) return;

    int ch = (s[pos] - 'a');

    if (cur -> next[ch] == 0)
        return;
    else {
        if (mem[cur -> next[ch]] < tt) {
            ans += cur -> next[ch] -> kol;
            mem[cur -> next[ch]] = tt;
        }

        check(cur -> next[ch], s, pos + 1);
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n;

    for (int i = 0; i < n; i++)
        cin >> s[i];

    sort(s, s + n, cmp);

    root = new trie();

    for (int i = 0; i < n; i++){
        tt++;

        for (int j = 0; j < sz(s[i]); j++)
            check(root, s[i], j);

        root -> insert(s[i], 0);
    }

    sort(s, s + n);

    for (int i = 0; i < n; ){
        int j = i;

        while (j < n && s[i] == s[j])
            j++;

        ll cur = j - i;

        ans += cur * (cur - 1ll) / 2ll;

        i = j;
    }

    cout << ans;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1024 KB Output is correct
2 Correct 5 ms 1024 KB Output is correct
3 Correct 5 ms 1024 KB Output is correct
4 Correct 5 ms 1024 KB Output is correct
5 Correct 7 ms 1536 KB Output is correct
6 Correct 8 ms 1664 KB Output is correct
7 Correct 9 ms 2304 KB Output is correct
8 Correct 9 ms 2816 KB Output is correct
9 Correct 25 ms 4224 KB Output is correct
10 Correct 32 ms 8704 KB Output is correct
11 Correct 35 ms 6656 KB Output is correct
12 Correct 58 ms 15968 KB Output is correct
13 Correct 57 ms 5624 KB Output is correct
14 Correct 50 ms 15864 KB Output is correct
15 Correct 67 ms 17552 KB Output is correct
16 Correct 52 ms 1656 KB Output is correct
17 Correct 35 ms 1016 KB Output is correct
18 Correct 36 ms 1024 KB Output is correct
19 Correct 54 ms 11000 KB Output is correct
20 Correct 38 ms 1576 KB Output is correct