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. // 12
  24. // ab ab ba ba ee ee ee ff ff ff gg gg
  25.  
  26. vector<string> a;
  27. void consistency(int n) {
  28.  
  29. unordered_map<string, int> mp, aa;
  30. int ans = 0;
  31.  
  32. for(auto& w : a){
  33. string rev(w);
  34. swap(rev[0], rev[1]);
  35.  
  36. if(w[0] == w[1]){
  37. aa[w]++;
  38. continue;
  39. }
  40.  
  41. if(mp.count(rev)){
  42. mp[rev]--;
  43. if(mp[rev]==0) mp.erase(rev);
  44. ans += 2;
  45. }
  46. else{
  47. mp[w]++;
  48. }
  49. }
  50.  
  51. int odd = 0;
  52. for(auto& it : aa){
  53. int ct = it.second;
  54. if(ct & 1){
  55. odd++;
  56. ans += ct-1;
  57. }
  58. else ans += ct;
  59. }
  60.  
  61. if(odd) ans++;
  62.  
  63. cout << 2*ans << endl;
  64.  
  65.  
  66. }
  67.  
  68. void solve() {
  69.  
  70. int n;
  71. cin >> n;
  72. a.resize(n);
  73. for(int i=0; i<n; i++) cin >> a[i];
  74.  
  75. consistency(n) ;
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83. int32_t main() {
  84. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  85.  
  86. int t = 1;
  87. while (t--) {
  88. solve();
  89. }
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0s 5328KB
stdin
12
ab ab ba ba ee ee ee ff ff ff gg gg
stdout
22