fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. struct by_length {
  7. bool operator() (const string& a, const string& b) const {
  8. if (a.size() != b.size()) {
  9. return a.size() < b.size();
  10. }
  11. return a < b;
  12. }
  13. };
  14.  
  15. int n;
  16. multiset<string, by_length> secik;
  17. map<string, int> cnt;
  18.  
  19. int main() {
  20.  
  21. ios_base::sync_with_stdio(0);
  22. cin.tie(0);
  23. cout.tie(0);
  24.  
  25. cin >> n;
  26. for (int i = 0; i < n; i++) {
  27. string s;
  28. cin >> s;
  29.  
  30. secik.insert(s);
  31. cnt[s]++;
  32. }
  33.  
  34. int ans = 0;
  35.  
  36. while (!secik.empty()) {
  37. string shortest = *secik.begin();
  38. secik.erase(secik.begin());
  39. ans = max(ans, (int)shortest.size());
  40.  
  41. if (cnt[shortest] > 1) {
  42. string new_string = shortest + shortest;
  43.  
  44. cnt[shortest] -= 2;
  45. secik.erase(secik.find(shortest));
  46.  
  47. secik.insert(new_string);
  48. cnt[new_string]++;
  49. }
  50. }
  51.  
  52. cout << ans;
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5284KB
stdin
3
bcd
olimpiada
bcd
stdout
9