持续更新——set,map
#include <bits/stdc++.h> #define endl '\n' #define x first #define y second #define int long long #define Tang ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N=1e5+10; const int INF=0x3f3f3f3f; const int mod=1e9+7; map <string,int> q; string a,b; int ans=0; void solve() { int n; cin >> n; while(n--) { cin >> a >> b; a=a.substr(0,2); if(a!=b) ans+=q[a+b]; q[b+a]++; } cout << ans << endl; } signed main() { Tang int T=1; //cin >> T; while(T--) solve(); return 0; }
#include <bits/stdc++.h> #define endl '\n' #define x first #define y second #define int long long #define Tang ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N=1e5+10; const int INF=0x3f3f3f3f; const int mod=1e9+7; map <int,map<int,int>> q; string a,b; int ans=0; void solve() { int n; cin >> n; while(n--) { cin >> a >> b; int x=a[0]*100+a[1]; int y=b[0]*100+b[1]; if(x!=y) { ans+=q[x][y]; q[y][x]++; } } cout << ans << endl; } signed main() { Tang int T=1; //cin >> T; while(T--) solve(); return 0; }