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