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.
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);
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
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??
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.
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.