Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Strange behaviour while comparing strings in c++
Разница между en1 и en2, 1 символ(ов) изменены
I was solving the problem[1800E2](https://codeforces.me/problemset/problem/1800/E2)↵

I came across strange behaviour while comparing strings in c++. My first submission failed on testcase 2, I changed the code to compare string and instead of using "==" wrote my comparator myself and it passed. I fail to understand my mistake in the first implementation.↵

submission 1: [Link](https://codeforces.me/contest/1800/submission/284398246)↵
 ↵
submission 2: [Link](https://codeforces.me/contest/1800/submission/284398393)↵

Basically my code included↵

~~~~~↵
string ta = a.substr(i, min(k, n) - i);↵
string tb = b.substr(i, min(k, n) - i);↵
 ↵
if(ta == tb) cout << "YES\n";↵
else cout << "NO\n";↵
~~~~~↵

which failed the testcases↵
then i changed this part of code to↵

~~~~~↵
for(int j = i; j < min(n, k); j++) {↵
    if(a[j] != b[j]) valid = 0;↵
}↵
~~~~~↵

This passed the testcase. I dont understand what was the problem in the first implementation, can anyone point something out that I am too dumb to understand. ↵

Sorry for my poor english, Thanks in advance.↵

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский ThreeKnives 2024-10-05 09:07:12 1
en1 Английский ThreeKnives 2024-10-05 09:06:54 1126 Initial revision (published)