how to input a 2d matrix using getchar_unlocked

how to input a 2d matrix using getchar_unlocked or the fastest method to do so.

I think the best way is to use scanf(). I solve many problems and I didn’t use getchar_unlocked() or other weird method. When you using scanf() and still getting TLE you doing something wrong. Because problem setters too use only scanf() or cin.

So in my opinion, the best way to read 2d matrix without spaces is this, because it’s fast enough, clear and easy.

char A[r][c];
for(int i=0; i<r; i++)
   for(int j=0; j<c; j++) scanf(" %c",&A[i][j]);

I hope, this help a bit, even when your main question is unanswered.

1 Like

Pls help me here, I took your code and tried it out



char A[2][2];
    int i,j;
    for( i=0; i<2; i++)
    for( j=0; j<2; j++) scanf("%c ",&A[i][j]);

    for( i=0; i<2; i++)
    for( j=0; j<2; j++) printf("%c ",A[i][j]);


At terminal it it accepting 3 lines of i/p as follows


ab
cd
ef

And it is printing: a b c d

Why so??

well first, you should only get 2 lines with 2 characters, nothing more will be read. But after that, program expect EOF, so push “Ctrl+D” which is EOF sequence. This is because space in scanf() command throw every blank character. Easiest way is use scanf like this: scanf(" %c",&A[i][j]);
It will do basicaly the same, but you don’t need EOF. Sorry for confusion.

Secondly, you don’t use printf correctly. What’s happening is print character A[i][j] and than one space. You should do it like:

for(i=0; i<2; i++) {
   for(j=0; j<2; j++) printf("%c",A[i][j]);
   printf("\n");
}

Hey, its thanks for the explanation…its working.
Can you please explain a little further as to what does a space in the scanf sequence do?? That is what does “throw every blank character” mean??

Dealing with characters.
Suppose you want to input something like this:

ababab
cdcdcd
efefef
ghghgh

A simple way will be to have an array or vector of strings in C++.
For the above example:

vector<string> s(4);     //this says a user-est. size of the vector.
for(int i=0; i<4; i++)
    cin>>s[i];

You can still access any character in this 2D array of characters, or 1D array of strings.

To print the 2nd c in 2nd row:

cout<<s[1][2];

This way, you need not worry about unnecessary trouble with characters.

EDIT: To speed up cin/cout, you can turn off the stdio sync

ios_base::sync_with_stdio(false);

If you got string " b" - one space and one ‘b’ and you use scanf("%c",&character) than in variable character will be space. But when you use scanf(" %c",&character) than character will contain ‘b’, because scanf throw away blank character - space. Even, when before ‘b’ is tabulator, enter or multiple of this blank characters, than they are skipped.

I like how both of you didn’t answer his question… He asked how to do it with getchar.

Anyway, unix\linux newline character is \n, while window’s is \n\r (g++ on codechef is tested under unix). Just keep this in mind when using getchar, you either have to separate these two cases or find an universal method.

Here’s an ideone to the code which should get the job done

Hi, I am a regular user of C, and hence have not used much of the C++ constructs, unless actually required. So, please ignore, if this question is silly!

If we need a vector, shouldn’t vector<string> s; be enough? Should we make an array of vectors?

You’re right. It was not intended to be an array of vectors. Earlier, it was a typo. And if we write vector s(4), then we are just estimating that we will be having some 4 strings in the vector. However by the property of vectors we can further reduce/enlarge the size.

I like how you ignored reading the second part of the question which is asking for an alternate method.