my_tempo's blog

By my_tempo, history, 4 years ago, In English

Link It's a simple problem with a simple solution, But still I'm not able to figure out why is there runtime error on a couple of test cases.

Program
  • Vote: I like it
  • +2
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +10 Vote: I do not like it

You should change the line if (s1.size() == s2.size()) return true; to if (s1.size() == s2.size()) return false;

Check out https://mirror.codeforces.com/blog/entry/57036?#comment-406911 for the reason.

»
4 years ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

Here and on a more practical note here. The second one is more useful, and it describes all of the conditions you need for a valid sorting order in C++. Comparators use a strict-weak ordering, which means that they should satisfy irreflexivity, transitivity, and transitivity of equivalence. Your comparator doesn't satisfy irreflexivity, so it sometimes throws a runtime error.