#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;
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 |
17 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1228 KB |
Output is correct |
2 |
Correct |
18 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
1484 KB |
Output is correct |
2 |
Correct |
42 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
1860 KB |
Output is correct |
2 |
Correct |
44 ms |
1520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
2316 KB |
Output is correct |
2 |
Correct |
78 ms |
2500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
8660 KB |
Output is correct |
2 |
Correct |
86 ms |
2116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
572 ms |
18716 KB |
Output is correct |
2 |
Correct |
149 ms |
2624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1016 ms |
34436 KB |
Output is correct |
2 |
Correct |
523 ms |
16464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1550 ms |
61060 KB |
Output is correct |
2 |
Correct |
311 ms |
4448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1527 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |