CF 1669E - 2-Letter Strings

2-Letter Strings

Given n n n strings, each of length 2 2 2, consisting of lowercase Latin alphabet letters from ‘a’ to ‘k’, output the number of pairs of indices ( i , j ) (i,j) (i,j) such that i < j i<j i<j and the i i i-th string and the j j j-th string differ in exactly one position.
In other words, count the number of pairs ( i , j ) ( i < j ) (i,j) (i<j) (i,j)(i<j) such that the i i i-th string and the j j j-th string have exactly one position p ( 1 ≤ p ≤ 2 ) p (1≤p≤2) p(1p2) such that s i p ≠ s j p s_{ip}≠s_{jp} sip=sjp.
The answer may not fit into 32-bit integer type, so you should use 64-bit integers like long long in C++ to avoid integer overflow.
Input
The first line of the input contains a single integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n ( 1 ≤ n ≤ 1 0 5 ) n (1≤n≤10^5) n(1n105) — the number of strings.
Then follows n n n lines, the i i i-th of which containing a single string s i s_i si of length 2 2 2, consisting of lowercase Latin letters from ‘a’ to ‘k’.
It is guaranteed that the sum of n over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, print a single integer — the number of pairs ( i , j ) ( i < j ) (i,j) (i<j) (i,j)(i<j) such that the i i i-th string and the j j j-th string have exactly one position p ( 1 ≤ p ≤ 2 ) p (1≤p≤2) p(1p2) such that s i p ≠ s j p s_{ip}≠s_{jp} sip=sjp.
Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
Example
input

4
6
ab
cb
db
aa
cc
ef
7
aa
bb
cc
ac
ca
bb
aa
4
kk
kk
ab
ab
5
jf
jf
jk
jk
jk

output

5
6
0
6

Note
For the first test case the pairs that differ in exactly one position are: (“ab”, “cb”), (“ab”, “db”), (“ab”, “aa”), (“cb”, “db”) and (“cb”, “cc”).
For the second test case the pairs that differ in exactly one position are: (“aa”, “ac”), (“aa”, “ca”), (“cc”, “ac”), (“cc”, “ca”), (“ac”, “aa”) and (“ca”, “aa”).
For the third test case, the are no pairs satisfying the conditions.

题目大意
找出所有由两个字符构成的字符串之中,只存在一个字符相同的字符串的对数(两字符串之间,一个字符相同,另一个不相同)
(注意:题目要求是前面的字符串与后面的字符串构成一对,也就避免了重复的现象)
解题思路
我们可以边输入边记录由两个字符构成的字符串出现的次数,然后寻找之前和他有一个字符不同的字符串的出现次数,将每次得到的出现次数加起来就是所要的对数。

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

int main()
{
    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        int sp[11][11]={0}; //记录两字符构成的字符串出现次数
        long long ans = 0;
        for(int i=0; i<n; i++)
        {
            char x, y; //获取两个字符
            scanf(" %c%c", &x, &y);
            sp[x-'a'][y-'a']++;
            for(int k=0; k<11; k++) //遍历 a~k 对应的下标
            {
                //第一个字符相同,第二字符不同的个数
                if(sp[x-'a'][k] && k!=y-'a')
                    ans += sp[x-'a'][k];
                //第一个字符不同,第二字符相同的个数
                if(sp[k][y-'a'] && k!=x-'a')
                    ans += sp[k][y-'a'];
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

花生ono

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

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

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

打赏作者

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

抵扣说明:

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

余额充值