Submission #232506

# Submission time Handle Problem Language Result Execution time Memory
232506 2020-05-17T08:54:30 Z VEGAnn Parametriziran (COCI19_parametriziran) C++14
77 / 110
179 ms 65540 KB
#include <bits/stdc++.h>
#define ft first
#define sd second
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 50100;
const int Z = 28;
string s[N], t;
int n, m;
ll ans = 0;

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

    trie(): kol(0) {
        fill(next, next + Z, nullptr);
    }

    void insert(string &cur, int id){
        kol++;

        if (id == m) return;

        int ch = 0;

        if (cur[id] == '#')
            ch = 26;
        else if (cur[id] == '?')
            ch = 27;
        else ch = (cur[id] - 'a');

//        cerr << next[ch] << '\n';

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

        next[ch] -> insert(cur, id + 1);
    }

    void calc(string &cur, int id){
        if (id == m){
            ans += kol;
            return;
        }

        if (cur[id] == '?'){
            if (next[26]) next[26] -> calc(cur, id + 1);
            if (next[27]) next[27] -> calc(cur, id + 1);
        } else {
            int ch = cur[id] - 'a';
            if (next[ch]) next[ch] -> calc(cur, id + 1);
            if (next[27]) next[27] -> calc(cur, id + 1);
        }
    }
};

trie* root;

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

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

    cin >> n >> m;

    root = new trie();

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

        for (int msk = 0; msk < (1 << m); msk++){
            bool bad = 0;
            t = "";

            for (int j = 0; j < m && !bad; j++)
                if (msk & (1 << j)) {
                    if (s[i][j] == '?')
                        bad = 1;
                    else t += "#";
                } else t += s[i][j];

            if (!bad)
                root -> insert(t, 0);
        }
    }

    for (int i = 0; i < n; i++)
        root -> calc(s[i], 0);

    cout << (ans - n) / 2;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 2176 KB Output is correct
2 Correct 11 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2176 KB Output is correct
2 Correct 16 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 5504 KB Output is correct
2 Correct 16 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 4736 KB Output is correct
2 Correct 30 ms 8832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 30800 KB Output is correct
2 Correct 30 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 179 ms 65528 KB Output is correct
2 Correct 37 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 129 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 99 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 96 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -