#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++) {
string str;
cin >> str;
a[i] = str;
}
for(int i = 0; i < n; i++) {
for(int mask = 0; mask < (1 << m); mask++) {
string s = a[i];
for(int j = 0; j < m; j++) {
if (mask & (1 << j)) {
s[j] = '*';
}
}
// cout << s << " " << hsh(s) << "\n";
cnt[hsh(s)]++;
}
}
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();
string str2 = str;
for(char& c : str2) {
if (c == '?') {
c = '*';
}
}
for(int mask = 0; mask < (1 << letters); mask++) {
string s = str2;
for(int j = 0; j < letters; j++) {
if (mask & (1 << j)) {
s[idx[j]] = '?';
}
}
// cout << s << " ";
// cout << cnt[s] << "\n";
ans += cnt[hsh(s)];
}
// 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 |
17 ms |
1228 KB |
Output is correct |
2 |
Correct |
14 ms |
1264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1484 KB |
Output is correct |
2 |
Correct |
26 ms |
1888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
1868 KB |
Output is correct |
2 |
Correct |
27 ms |
1524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
2368 KB |
Output is correct |
2 |
Correct |
58 ms |
2328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
7692 KB |
Output is correct |
2 |
Correct |
66 ms |
2100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
445 ms |
20716 KB |
Output is correct |
2 |
Correct |
109 ms |
2464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
653 ms |
29072 KB |
Output is correct |
2 |
Correct |
319 ms |
14320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1295 ms |
52244 KB |
Output is correct |
2 |
Correct |
259 ms |
3952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1322 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |