Submission #444479

# Submission time Handle Problem Language Result Execution time Memory
444479 2021-07-14T07:28:02 Z zxcvbnm Parametriziran (COCI19_parametriziran) C++14
99 / 110
1262 ms 65540 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int p = 31;
const int mod = 1e9 + 9;

ll hsh(const string& s) {
    ll ret = 0;
    ll p_pow = 1;

    for(char c : s) {
        int ch = 26;
        if (isalpha(c)) {
            ch = c - 'a';
        } else if (c == '*') {
            ch = 27;
        }

//        cout << c << " " << ch << "\n";
        ret = (ret + ch * p_pow) % mod;
        p_pow = (p_pow * p) % mod;
    }

    return ret;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    unordered_map<int, int> cnt;
    vector<string> a(n);

    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for(int i = 0; i < n; i++) {
        string str = a[i];
        for(int mask = 0; mask < (1 << m); mask++) {
            for(int j = 0; j < m; j++) {
                if (mask & (1 << j)) {
                    str[j] = '*';
                }
            }
            cnt[hsh(str)]++;
            for(int j = 0; j < m; j++) {
                if (mask & (1 << j)) {
                    str[j] = a[i][j];
                }
            }
        }
    }

    ll ans = 0;
    for(string str : a) {
        vector<int> idx;
        for(int i = 0; i < m; i++) {
            if (isalpha(str[i])) {
                idx.push_back(i);
            }
        }

        int letters = idx.size();
        for(char& c : str) {
            if (c == '?') {
                c = '*';
            }
        }
        string str2 = str;

        for(int mask = 0; mask < (1 << letters); mask++) {
            for(int j = 0; j < letters; j++) {
                if (mask & (1 << j)) {
                    str[idx[j]] = '?';
                }
            }
            ans += cnt[hsh(str)];
            for(int j = 0; j < letters; j++) {
                if (mask & (1 << j)) {
                    str[idx[j]] = str2[idx[j]];
                }
            }
        }
//        cerr << "\n";
    }

    ans -= n;
    ans /= 2;
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1228 KB Output is correct
2 Correct 12 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1484 KB Output is correct
2 Correct 23 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 1796 KB Output is correct
2 Correct 24 ms 1456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 2408 KB Output is correct
2 Correct 45 ms 2256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 7740 KB Output is correct
2 Correct 57 ms 2192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 420 ms 20868 KB Output is correct
2 Correct 94 ms 2404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 625 ms 29056 KB Output is correct
2 Correct 325 ms 14320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1262 ms 52356 KB Output is correct
2 Correct 231 ms 3956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1233 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -