#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int p = 31;
const int mod = 1e9 + 9;
int 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++) {
cin >> a[i];
}
for(int i = 0; i < n; i++) {
string str = a[i];
for(int mask = 0; mask < (1 << m); mask++) {
for(int j = 0; j < m; j++) {
if (mask & (1 << j)) {
str[j] = '*';
}
}
cnt[hsh(str)]++;
for(int j = 0; j < m; j++) {
if (mask & (1 << j)) {
str[j] = a[i][j];
}
}
}
}
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();
for(char& c : str) {
if (c == '?') {
c = '*';
}
}
string str2 = str;
for(int mask = 0; mask < (1 << letters); mask++) {
for(int j = 0; j < letters; j++) {
if (mask & (1 << j)) {
str[idx[j]] = '?';
}
}
ans += cnt[hsh(str)];
for(int j = 0; j < letters; j++) {
if (mask & (1 << j)) {
str[idx[j]] = str2[idx[j]];
}
}
}
// cerr << "\n";
}
ans -= n;
ans /= 2;
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
1228 KB |
Output is correct |
2 |
Correct |
13 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
1612 KB |
Output is correct |
2 |
Correct |
23 ms |
1896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
1764 KB |
Output is correct |
2 |
Correct |
24 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
2324 KB |
Output is correct |
2 |
Correct |
44 ms |
2332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
7836 KB |
Output is correct |
2 |
Correct |
58 ms |
2176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
338 ms |
20720 KB |
Output is correct |
2 |
Correct |
98 ms |
2436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
590 ms |
29088 KB |
Output is correct |
2 |
Correct |
300 ms |
14320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1142 ms |
52292 KB |
Output is correct |
2 |
Correct |
218 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1198 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |