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
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | orzdevinwang | 3844 |
3 | jqdai0815 | 3682 |
4 | jiangly | 3618 |
5 | Benq | 3529 |
6 | ksun48 | 3489 |
7 | Radewoosh | 3483 |
8 | Kevin114514 | 3443 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | atcoder_official | 162 |
3 | maomao90 | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
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.
string s;
map<char, int> pos;
bool comp(string s1, string s2) {
int len = min(s1.size(), s2.size());
for (int i = 0; i < len; ++i) {
if (pos[s1[i]] < pos[s2[i]]) return true;
else if (pos[s1[i]] > pos[s2[i]]) return false;
}
if (s1.size() == s2.size()) return true;
else {
if (len == s1.size()) return true;
else return false;
}
}
void solve()
{
cin >> s;
for (int i = 0; i < 26; ++i) {
pos[s[i]] = i;
pos['A' + s[i] - 'a'] = i + 26;
}
int n; cin >> n;
vector<string> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end(), comp);
for (int i = 0; i < n; ++i) cout << a[i] << endl;
cout << endl;
}
Название |
---|
You should change the line
if (s1.size() == s2.size()) return true;
toif (s1.size() == s2.size()) return false;
Check out https://codeforces.me/blog/entry/57036?#comment-406911 for the reason.
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.