思路:
统计所有相同字符串的个数
寻找和本身字符串数字完全相反的字符串
两个字符串个数相乘
将所有相乘的和加起来
大佬解题思路:用map<string,int>cnt 存
注意:map<string, int>中的键-值
完整代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
map<string, int> cnt;
int main()
{
int n, m, sum = 0;
cin >> n >> m;
for(int i=0; i<n; i++)
{
string s = ""; //新的键值对应,防止所有数据集合为一个字符串
for(int j=0; j<m; j++)
{
//将第n个人的打分记下为一个字符串
char x;
cin >> x;
s+=x;
}
cnt[s]++; //相同评分的+1
}
for(map<string, int>::iterator it=cnt.begin(); it!=cnt.end(); it++)
{
//cout << it->first << ' ' << it->second << endl;
//这里查看有没有相反的,即求出第n个的相反的评分,然后根据键值查看
string s="";
for(int i=0; i<m; i++)
{
if(it->first[i] == '0')
{
s += "1";
}
else
{
s += "0";
}
}
//cout << it->first << ' ' << it->second << endl;
//cout << "s=" << s << ' ' << "cnt[s]= " << cnt[s] << "it->sexond= " << it->second << endl;
//这里表示若有相反的,即键值和it->second不为0,存到sum中,但是由于相反事互相的,所以后面要除以2
sum += cnt[s]*it->second; //it->first表示 map<string,int>中的string部分,it->second表示map<string,int>中的int部分
}
cout << sum/2 << endl;
return 0;
}
//#include <iostream>
//#include <map>
//#include <algorithm>
//using namespace std;
//map<string, int> cnt;
//int main()
//{
// for(int j=0; j<2; j++)
// {
// string s="";
// for(int i=0; i<3; i++)
// {
// char x;
// cin >> x;
// s+=x;
// }
// cnt[s]++;
// }
// for(map<string, int>::iterator it=cnt.begin(); it!=cnt.end(); it++)
// {
// cout << it->first << ' ' << it->second << endl;
// }
// return 0;
//}