fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<string> a;
  26. void consistency(int n) {
  27.  
  28. unordered_map<string, int> mp, aa;
  29. int ans = 0;
  30.  
  31. for(auto& w : a){
  32. string rev(w);
  33. swap(rev[0], rev[1]);
  34.  
  35. if(w[0] == w[1]){
  36. aa[w]++;
  37. continue;
  38. }
  39.  
  40. if(mp.count(rev)){
  41. mp[rev]--;
  42. if(mp[rev]==0) mp.erase(rev);
  43. ans += 2;
  44. }
  45. else{
  46. mp[w]++;
  47. }
  48. }
  49.  
  50. int odd = 0;
  51. for(auto& it : aa){
  52. int ct = it.second;
  53. if(ct & 1){
  54. odd++;
  55. ans += ct-1;
  56. }
  57. else ans += ct;
  58. }
  59.  
  60. if(odd) ans++;
  61.  
  62. cout << 2*ans << endl;
  63.  
  64.  
  65. }
  66.  
  67. void solve() {
  68.  
  69. int n;
  70. cin >> n;
  71. a.resize(n);
  72. for(int i=0; i<n; i++) cin >> a[i];
  73.  
  74. consistency(n) ;
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int32_t main() {
  83. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  84.  
  85. int t = 1;
  86. while (t--) {
  87. solve();
  88. }
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0.01s 5320KB
stdin
12
ab ab ba ba ee ee ee ff ff ff gg gg
stdout
22