Submission #96616

# Submission time Handle Problem Language Result Execution time Memory
96616 2019-02-10T13:00:38 Z FutymyClone Parametriziran (COCI19_parametriziran) C++14
77 / 110
3000 ms 35884 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e4 + 5;

int n, m;
long long ans = 0;

struct Trie {
    Trie *child[27];
    int last;
    Trie(){
        last = 0;
        for (int i = 0; i < 27; i++) child[i] = NULL;
    }
} *root = new Trie();

void dfs (string s, int pos, Trie *cur) {
    if (pos == m) {
        cur -> last++;
        return;
    }

    if (s[pos] == '?') {
        if (cur -> child[26] == NULL) cur -> child[26] = new Trie();
        dfs(s, pos + 1, cur -> child[26]);
    }
    else {
        if (cur -> child[s[pos] - 'a'] == NULL) cur -> child[s[pos] - 'a'] = new Trie();
        dfs(s, pos + 1, cur -> child[s[pos] - 'a']);
    }
}

void solve (string s, int pos, Trie *cur) {
    if (pos == m) {
        ans += cur -> last;
        return;
    }

    if (s[pos] == '?') {
        for (int i = 0; i < 27; i++) {
            if (cur -> child[i] != NULL)
            solve(s, pos + 1, cur -> child[i]);
        }
    }
    else {
        if (cur -> child[s[pos] - 'a'] != NULL)
        solve(s, pos + 1, cur -> child[s[pos] - 'a']);
        if (cur -> child[26] != NULL)
        solve(s, pos + 1, cur -> child[26]);
    }
}

int main(){
    //freopen("testgen.inp", "r", stdin);
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        string s; cin >> s;
        solve(s, 0, root);
        dfs(s, 0, root);
    }

    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 504 KB Output is correct
2 Correct 26 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 580 KB Output is correct
2 Correct 22 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 3408 KB Output is correct
2 Correct 297 ms 1240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 472 ms 2508 KB Output is correct
2 Correct 782 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 307 ms 14208 KB Output is correct
2 Correct 1701 ms 1664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1168 ms 16192 KB Output is correct
2 Correct 2126 ms 2056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 23032 KB Output is correct
2 Execution timed out 3060 ms 5956 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 279 ms 21716 KB Output is correct
2 Execution timed out 3063 ms 2576 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1572 ms 35884 KB Output is correct
2 Execution timed out 3058 ms 7404 KB Time limit exceeded