一道哈希題,
因為只有兩個關鍵字,直接用進制哈希,
然后存到一個陣列里面,每次邊更新答案邊哈希
代碼
#include<cstdio>
using namespace std;
int n;
char a[100],b[100];
int get1(int i){//對城市開頭的哈希
return (((a[0]-'A')*26+a[1]-'A')*26+b[0]-'A')*26+b[1]-'A';
}
int get2(int i){//對洲代碼開頭的哈希
return (((b[0]-'A')*26+b[1]-'A')*26+a[0]-'A')*26+a[1]-'A';
}
int f[500000];
int main(){
scanf("%d",&n);
long long ans=0;
for(int i=1;i<=n;i++){
scanf("%s%s",a,b);
if(get1(i)!=get2(i)){
ans+=f[get2(i)];//要先更新答案,因為如果自己可以和自己對上,那么就不行了
f[get1(i)]++;
}
}
printf("%lld",ans);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/159308.html
標籤:其他
上一篇:學習方波有霍爾傳感的電機調速記錄
