fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct sort_by_length {
  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. int n;
  18. cin >> n;
  19.  
  20. multiset<string, sort_by_length> secik;
  21. map<string, int> cnt;
  22.  
  23. for (int i = 0; i < n; i++) {
  24. string s;
  25. cin >> s;
  26.  
  27. secik.insert(s);
  28. cnt[s]++;
  29. }
  30.  
  31. int odp = 0;
  32. while (!secik.empty()) {
  33. string shortest = *secik.begin();
  34. odp = max(odp, (int)shortest.size());
  35. secik.erase(secik.begin());
  36.  
  37. if (cnt[shortest] > 1) {
  38. secik.erase(secik.find(shortest));
  39. cnt[shortest] -= 2;
  40.  
  41. string new_string = shortest + shortest;
  42. secik.insert(new_string);
  43. cnt[new_string]++;
  44. }
  45. }
  46.  
  47. cout << odp;
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5300KB
stdin
3
bcd
olimpiada
bcd
stdout
9