Very puzzling. The only situations that I can think of where this would make a difference is when the input is misformatted (not uncommon with External Contests), but in that case I’d expect that the char array version would give the correct answer - unless the expected output was wrong, too XD
Edit: Unless one of the lines contained a string that was longer than m - that would favour the string solution over the char one, I think.
Let’s rule out some possibilities. Can you submit the following and post a link to the submission? It will either give WA or RE.
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
string line;
cin >> line;
assert(line.size() == m);
}
assert(cin);
}
Thanks! Let’s narrow it down further. If my “Edit” in the post above is on the mark, this will also be a RE; please submit and post the link
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
string line;
cin >> line;
assert(line.size() <= m);
}
}
Aha - so at least one line in the test input is longer than it’s supposed to be, and this will favour the string solution.
For example, let’s take the sample test input:
3 3
abc
elm
xet
Imagine if we accidentally added a character to one of the strings:
3 3
abc
elmz
xet
The version using string would give the same answer as for the “correct” version of this test input, whereas the version using char arrays would give the wrong answer.
But in the solution in both cases i am accessing m characters of the string, so should’nt it be same thing or the expected output is also wrong bcoz of this?