This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |