Submission #250211

# Submission time Handle Problem Language Result Execution time Memory
250211 2020-07-17T08:11:57 Z Vladikus004 Lozinke (COCI17_lozinke) C++14
100 / 100
618 ms 13688 KB
#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;

const int N = 20000 + 3, P = 31;
int n;
string s[N];
ull h[11],deg[11];

void make_hash(string &s){
    h[0] = s[0] - 'a' + 1;
    deg[0] = 1;
    for (int i = 1; i < s.size(); i++){
        h[i] = h[i - 1] * P + (s[i] - 'a' + 1);
        deg[i] = deg[i - 1] * P;
    }
}

ull get_hash(int l, int r){
    if (!l) return h[r];
    return h[r] - h[l - 1] * deg[r - l + 1];
}

map <ull, int> mp;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> s[i];
    }
    sort(s, s + n);
    reverse(s, s + n);
    int ans = 0;
    for (int i = 0; i < n; i++){
        make_hash(s[i]);
        set <ull> ms;
        for (int j = 0; j < s[i].size(); j++){
            for (int k = j; k < s[i].size(); k++){
                ms.insert(get_hash(j, k));
            }
        }
        ans += mp[get_hash(0, s[i].size() - 1)];
        for (auto u: ms){
            mp[u]++;
        }
    }
    mp.clear();
    reverse(s, s + n);
    for (int i = 0; i < n; i++){
        make_hash(s[i]);
        set <ull> ms;
        for (int j = 0; j < s[i].size(); j++){
            for (int k = j; k < s[i].size(); k++){
                ms.insert(get_hash(j, k));
            }
        }
        ans += mp[get_hash(0, s[i].size() - 1)];
        for (auto u: ms){
            mp[u]++;
        }
    }
    cout << ans;
}

Compilation message

lozinke.cpp: In function 'void make_hash(std::__cxx11::string&)':
lozinke.cpp:18:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < s.size(); i++){
                     ~~^~~~~~~~~~
lozinke.cpp: In function 'int main()':
lozinke.cpp:48:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < s[i].size(); j++){
                         ~~^~~~~~~~~~~~~
lozinke.cpp:49:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int k = j; k < s[i].size(); k++){
                             ~~^~~~~~~~~~~~~
lozinke.cpp:63:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < s[i].size(); j++){
                         ~~^~~~~~~~~~~~~
lozinke.cpp:64:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int k = j; k < s[i].size(); k++){
                             ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1024 KB Output is correct
2 Correct 1 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 2 ms 1024 KB Output is correct
5 Correct 9 ms 1280 KB Output is correct
6 Correct 14 ms 1280 KB Output is correct
7 Correct 21 ms 1792 KB Output is correct
8 Correct 36 ms 2560 KB Output is correct
9 Correct 74 ms 2448 KB Output is correct
10 Correct 218 ms 6392 KB Output is correct
11 Correct 133 ms 3704 KB Output is correct
12 Correct 526 ms 13688 KB Output is correct
13 Correct 195 ms 2296 KB Output is correct
14 Correct 387 ms 12304 KB Output is correct
15 Correct 618 ms 13432 KB Output is correct
16 Correct 143 ms 1144 KB Output is correct
17 Correct 57 ms 1016 KB Output is correct
18 Correct 42 ms 896 KB Output is correct
19 Correct 304 ms 7208 KB Output is correct
20 Correct 81 ms 1024 KB Output is correct