P3405 [USACO16DEC] Cities and States S
城市的前两位字符有效,已知字符串均为大写字母构成,故可转26进制储存,不会重复。
city[M][M] 存入:行为国家列为州 若存在与另一组数据对应为city 1 == state 2 && state 1 == city 2;ans++;
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int M = 702;
int city[M][M] = {0},ans = 0;
int main()
{
string x, y;
memset(city, 0, sizeof city);
int n;
cin>>n;
while(n--){
cin>>x>>y;
int q = (x[0]-'A')*26+x[1]-'A';
int p = (y[0]-'A')*26+y[1]-'A';
if(p != q){
city[q][p]++;
ans+=city[p][q];
}
}
cout<<ans;
}
浙公网安备 33010602011771号