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(1≤p≤2) 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(1≤t≤100) — 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(1≤n≤105) — 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(1≤p≤2) 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
input4 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;
}