#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
#define forn for(int i=1;i<=n;i++)
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define pb push_back
#define sp setprecision
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define int long long
#define ll long long
#define nn '\n'
using namespace std;
const int N=1e6;
const int M=105*105;
const int INF=1e18;
const int MOD=1e9+7;
int n,d;
string s[N];
void al(){
cin>>n>>d;
for(int i=1;i<=n;i++){
cin>>s[i];
}
if(d==4){
int x=((n-1)*n)/2;
cout<<x;
return;
}
if(n<=2000){
int ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
int cnt=0;
for(int k=0;k<4;k++){
if(s[i][k]!=s[j][k]) cnt++;
}
if(cnt<=d) ans++;
}
}
cout<<ans;
return;
}
map<string,int>mp;
for(int i=1;i<=n;i++){
int sz=(1<<4);
for(int mask=0;mask<sz;mask++){
int x=__builtin_popcount(mask);
if(x!=d) continue;
else {
string st=s[i];
for(int j=0;j<4;j++){
int k=(mask>>j)&1;
if(k==1) {
st[j]=')';
}
}
mp[st]++;
}
}
}
int cnt=0;
for(int i=1;i<=n;i++){
int sz=(1<<4);
for(int mask=0;mask<sz;mask++){
int x=__builtin_popcount(mask);
if(x!=d) continue;
else {
string st=s[i];
for(int j=0;j<4;j++){
int k=(mask>>j)&1;
if(k==1) {
st[j]=')';
}
}
cnt+=mp[st]-1;
}
}
}
cout<<cnt/2;
}
/*
5 1 0
a a с с с с
2 2 0 0 0 0
4
8
*/
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout.tie(0);
int t=1;
// cin>>t;
while(t--) {
al();
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |