It would help immensely if you had included a link to the specific question. Then we could advise if you had answered the question correctly.
For instance, what about capital letters, punctuation, etc.?
For instance, does the question guarantee that each input string will <= 100 characters, or is there a criteria that no more than the first 100 characters be used?
We cannot determine if the posted code is answering the question if we do not know what the question is.
Most I/O is very time/CPU cycles expensive, especially scanf() and printf()
Most online questions have the execution timed and exceeding the allowed time limit will cause the proposed answer (that you provide) to fail.
Appropriate 'fast' I/O functions are easily found by YOU by looking at the answers to prior questions. Here are a couple of fully functional examples to get you started:
#include <stdio.h>
void fastRead( size_t *a );
void fastWrite( size_t a );
inline void fastRead(size_t *a)
{
int c=0;
// note: 32 is space character
while (c<33) c=getchar_unlocked();
// initialize result value
*a=0;
// punctuation parens, etc are show stoppers
while (c>47 && c<58)
{
*a = (*a)*10 + (size_t)(c-48);
c=getchar_unlocked();
}
//printf( "%s, value: %lu\n", __func__, *a );
} // end function: fastRead
inline void fastWrite(size_t a)
{
char snum[20];
//printf( "%s, %lu\n", __func__, a );
int i=0;
do
{
// 48 is numeric character 0
snum[i++] = (char)((a%10)+(size_t)48);
a=a/10;
}while(a>0);
i=i-1; // correction for overincrement from prior 'while' loop
while(i>=0)
{
putchar_unlocked(snum[i--]);
}
putchar_unlocked('\n');
} // end function: fastWrite
Most often (you may have to experiment a bit) the online code judging wants a newline output to stdout after each test case, including the last test case. This is usually accomplished by:
putchar_unlocked('\n');
When a value will never be less than 0, it is (usually) best to use size_t rather than int,
It is good programming practice to use variable names that indicate content or usage (or better, both).
code is MUCH more readable and understandable if
- it is consistently indented
- code blocks are separated by a single blank line
- meaningful variable names are used
- all 'distractor' code is eliminated
Putting all the above together results in:
#include <stdio.h> // putchar_unlocked(), getchar_unlocked()
void fastRead( size_t *a );
void fastWrite( size_t a );
inline void fastRead(size_t *a)
{
int c=0;
// note: 32 is space character
while (c<33) c=getchar_unlocked();
// initialize result value
*a=0;
// punctuation parens, etc are show stoppers
while (c>47 && c<58)
{
*a = (*a)*10 + (size_t)(c-48);
c=getchar_unlocked();
}
//printf( "%s, value: %lu\n", __func__, *a );
} // end function: fastRead
inline void fastWrite(size_t a)
{
char snum[20];
//printf( "%s, %lu\n", __func__, a );
int i=0;
do
{
// 48 is numeric character 0
snum[i++] = (char)((a%10)+(size_t)48);
a=a/10;
}while(a>0);
i=i-1; // correction for overincrement from prior 'while' loop
while(i>=0)
{
putchar_unlocked(snum[i--]);
}
putchar_unlocked('\n');
} // end function: fastWrite
int main ( void )
{
size_t numTestCases;
fastRead( &numTestCases );
for( size_t testCase =1; testCase <= numTestCases; testCase++)
{
size_t count = 0;
int ch;
while( (ch = getchar_unlocked()) != '\n' )
{
if( ch == 'a'
|| ch == 'd'
|| ch == 'g'
|| ch == 'j'
|| ch == 'm'
|| ch == 'p'
|| ch == 't'
|| ch == 'w'
|| ch == ' ')
{
count += 1;
}
else if( ch == 'b'
|| ch == 'e'
|| ch == 'h'
|| ch == 'k'
|| ch == 'n'
|| ch == 'q'
|| ch == 'u'
|| ch == 'x')
{
count += 2;
}
else if( ch == 'c'
|| ch == 'f'
|| ch == 'i'
|| ch == 'l'
|| ch == 'o'
|| ch == 'r'
|| ch == 'v'
|| ch == 'y')
{
count += 3;
}
else if( ch == 's'
|| ch == 'z')
{
count += 4;
}
}
printf("Case #%lu: %lu\n", testCase, count);
putchar_unlocked( '\n' );
}
return 0;
}
The call to printf() could also be greatly sped up by using a series of calls to fastWrite() and putchar_unlocked()
Also, unless the question specifically asks for the superfluous characters being used in the call to printf() the code probably should say:
fastWrite( testCase );
putchar_unlocked( ' ' );
fastWrite( count );
putchar_unlocked( '\n' );
it is also highly desirable to list the parameters of the question at the top of your code. This both helps you and anyone else reading your code.
Here is a trivial example of such documentation:
/*
* criteria 1
* Using two characters: . (dot) and * (asterisk)
* print a frame-like pattern
*
* criteria 2
* You are given t - the number of test cases
*
* criteria 3
* for each of the test cases input two positive integers:
* l - the number of lines and
* c - the number of columns of a frame.
*
* criteria 4
* Use one line break in between successive patterns
*/
safter%[^\n]? That can't work, becaue%[^\n]stops reading when it gets to a newline character, and then the next character can't besbecause it's newline.scanf("%[^\n]s", s);->scanf("%100[^\n]", s);- As per the manual page. Also perhaps usingswitchmight make the code more readablescanf("%[^\n]s", s);would do?