题目地址:https://codeforces.com/contest/1287/problem/B
n行字符串,每个字符串k个字符,每个字符串只能是’S’、‘T’、'E’这三个字符,三个字符串算一组,问有多少组。
3 3
SET
ETS
TSE
//第一列SET都不一样,第二列ETS都不一样,第三列TSE都不一样
//所以这三个字符串算1组
3 4
SETE
ETSE
TSES
//第一列SET都不一样,第二列ETS都不一样,第三列TSE都不一样,第四列EES
//有两个一样的,所以这三个字符串不算一组
解决方法:输入时map存储,暴力枚举前两个,找出那个唯一的第三个字符,map查询该字符是否存在,存在则加上
AC代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
#define dd double
#define PI acos(-1.0)
#define f(i, x, y) for(ll i = x; i < y; i++)
using namespace std;
const ll MAXN = 2e5 + 5;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, k; cin >> n >> k;
string s[1505];
map<string, ll> ma;
f(i, 0, n) {
cin >> s[i];
ma[s[i]]++;
}
ll cnt = 0;
f(i, 0, n - 1) {
f(j, i + 1, n) {
string ss = "";
f(x, 0, k) {
if (s[i][x] == s[j][x]) {
ss += s[i][x];
}
else {
if (s[i][x] == 'S') {
if (s[j][x] == 'E') {
ss += 'T';
}
else if (s[j][x] == 'T') {
ss += 'E';
}
}
else if (s[i][x] == 'E') {
if (s[j][x] == 'S') {
ss += 'T';
}
else if (s[j][x] == 'T') {
ss += 'S';
}
}
else {
if (s[j][x] == 'S') {
ss += 'E';
}
else if (s[j][x] == 'E') {
ss += 'S';
}
}
}
}
if (ma[ss] > 0) {
cnt += ma[ss];
}
}
}
cout << cnt / 3<< endl;
}