Codeforces Round #612 (Div. 2) B.Hyperset

81 篇文章 1 订阅
62 篇文章 1 订阅

Codeforces Round #612 (Div. 2) B.Hyperset

Bees Alice and Alesya gave beekeeper Polina famous card game “Set” as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.
Polina came up with a new game called “Hyperset”. In her game, there are n cards with k features, each feature has three possible values: “S”, “E”, or “T”. The original “Set” game can be viewed as “Hyperset” with k=4.
Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.
Unfortunately, winter holidays have come to an end, and it’s time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.

Input

The first line of each test contains two integers n and k (1≤n≤1500, 1≤k≤30) — number of cards and number of features.

Each of the following n lines contains a card description: a string consisting of k letters “S”, “E”, “T”. The i-th character of this string decribes the i-th feature of that card. All cards are distinct.

Output

Output a single integer — the number of ways to choose three cards that form a set.

Examples

input

3 3
SET
ETS
TSE

output

1

input

3 4
SETE
ETSE
TSES

output

0

input

5 4
SETT
TEST
EEET
ESTE
STES

output

2

比赛时读错题意了,根本不知道怎么优化,后来发现每一个特征,要么都相同,要么都不同,那么就很好写了,先用一个map把每个字符串标记一下,后面直接找就可以了,注意答案要除三,因为跑n2时每组都重复了三次,复杂度k*n2logn,又是一次悲惨掉分局😭

#include <bits/stdc++.h>
using namespace std;

char f(char a,char b){
    int s='S'+'E'+'T';
    if (a==b) return a;
    else return s-a-b;
}

int main()
{
    int n,k;
    cin>>n>>k;
    string s[1505];
    map<string,int>m;
    for(int i=0;i<n;i++){
        cin>>s[i];
        m[s[i]]=1;
    }
    int cnt=0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            string ss="";
            for(int u=0;u<k;u++){
                ss+=f(s[i][u],s[j][u]);
            }
          if(m[ss]) cnt++;
        }
    }
    cout<<cnt/3<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值