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 sz(c) int(c.size())
#define rep(i,a,b) for (int i=a; i<(b); ++i)
#define per(i,a,b) for (int i=(b)-1; i>=(a); --i)
using namespace std;
using ll = long long;
using ull = unsigned long long;
int const MAXN=55000;
int N,M;
string words[MAXN];
unordered_map<ull,int> cnt;
int val[512];
ull enc(const string &s) {
ull res=0;
rep(i,0,M) {
res=res*28+val[int(s[i])];
}
return res;
}
ll count_pairs(string s) {
ll res=0;
string t=s;
rep(i,0,M) if (s[i]=='?') s[i]='*';
rep(mask,0,1<<M) {
rep(i,0,M) t[i]=(mask>>i&1) ? '?' : s[i];
ull et=enc(t);
if (cnt.count(et)) res+=cnt[et];
}
return res;
}
void add_word(string s) {
string t=s;
rep(mask,0,1<<M) {
bool ok=true;
rep(i,0,M) {
if ((mask>>i&1) && s[i]=='?') {
ok=false;
break;
}
t[i]=(mask>>i&1) ? '*' : s[i];
}
if (ok) {
cnt[enc(t)]++;
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout<<fixed<<setprecision(10);
memset(val,~0,sizeof(val));
rep(i,0,26) val['a'+i]=i;
val['*']=26;
val['?']=27;
cin>>N>>M;
rep(i,0,N) cin>>words[i];
ll res=0;
rep(i,0,N) {
// cerr<<count_pairs(words[i])<<endl;
res += count_pairs(words[i]);
add_word(words[i]);
}
cout<<res<<"\n";
}
# | 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... |