NOLOGIC - Editorial

#include
#include
#include
using namespace std;
int main()
{
int t;
char question[320];
bool x[26]={false};
int i;
scanf("%d",&t);
while(t–)
{
gets(question);
for(i=0;i<(int)strlen(question);++i)
{
if(isalpha(question[i]))
{
if(islower(question[i]))
x[question[i]-97]=true;
else
x[question[i]-65]=true;
}
}
bool flag=true;
for(i=0;i<26;++i)
{
if(x[i]==false)
{
printf("%c\n",i+65);
flag=false;
break;
}
}
if(flag) printf("~\n");
}
return 0;
}

I don’t know why this code is not working can any one tell why?

1 Like

@amitupadhyay
Your solution has several mistakes.

  1. You should read a new-line character after scanf("%d",&t) as explained in the editorial.
  2. Declaration of bool x[26]={false}; should be moved inside while-loop.

By simply fixing this I’ve got AC:
http://www.codechef.com/viewsolution/1853065

But your code also has another bad place.
You calculate strlen(question) each time in the loop.
Note that strlen() function works in O(N) time.
So your solution is actually has complexity O(N * N).
If constraints would be higher (like question length up to 31415) you would probably get TLE.
It is always better to save the length at some variable like int this fix of your solution:
http://www.codechef.com/viewsolution/1853057

3 Likes

@anton_lunyov My bad :frowning: thankyou very much

it might be the dumb question but i want to know what’s wrong in writing if(‘a’ <= s[i] <= ‘z’) (than if(‘a’ <= s[i] && s[i] <= ‘z’)) in c? I am getting wrong answer for first case.

Expression ('a' <= s[i] <= 'z') is equivalent to ('a' <= s[i]) <= 'z'.
So it compares bool value ('a' <= s[i]) with char value 'z'.
Hence, it casts both values to int: 'z' to 122 and
('a' <= s[i]) to 0 if it is false or to 1 if it is true.
So ('a' <= s[i] <= 'z') is always true :slight_smile:

1 Like

@anton_lunyov i have been using scanf("%d\n",&t) blindly without knowing how it is working.
“The \n format will skip all whitespaces until it finds a non-whitespace character” . why does it skips?

also please explain %d%*c format. what is %*c doing ?

P.S. the ascii character 92 and ‘*’ is not working in comments here.

I seriously don’t feel the above explaination you gave justifies this being rejected

StringBuilder out=new StringBuilder();
while(t-->0){
    // ...
    out.append(ans+" \n");
}
System.out.println(out);

it is, but you have to realize what markdown formatting is doing with it…

Your code is not working, see the test case here - http://ideone.com/LTvWXq

but if you are using such low level functions you should handle case in which line is ending with \r\n

@aichemzee: your code simply contains T+1 x \n and as stated in statement

Special judge is very strict for this problem and check your output character-by-character. Be sure that you follow the mentioned output format precisely. For instance, your output should contain exactly T new-line characters (ASCII code 10) - one after each answer.

so it’s incorrect…

@anton_lunyov thanks for such problem, I hope a lot of coders learnt lesson in this contest

If it’s specified in statement so precisely as in this I think it is perfectly fine to reject such submissions.

On the other hand I prefer no so strict judge, for example if one have to print N numbers in line, typically it’s ok when there is space after the last one or so, because one can use FOR(i,0,N) printf("%d ", a[i]) instead of printf("%d", a[0]); FOR(i,1,N) printf(" %d", a[i]) and AFAIK in ACM contest there is special judgement result “wrong format” that is shown sometime in such cases…

@betlista As I said above try to solve something on UVA.
After struggling to solve some simple problem you will reconsider your opinion :slight_smile:
To output N space separated integers you could use code
FOR(i,0,N) printf("%d%c", a[i], i<N-1?' ':'\n')
So nothing ugly.

@aichemzee Usually special judge at codechef ignores extra white spaces.
So not need to worry about that in the future.

Actually both ways with scanf("%d%*c") and with getchar() do not handle the case with '\r\n'.
So the most general way is to use gets() or do getchar() / scanf("%c") in a loop.

@shimanshu Refer to this:
http://www.cplusplus.com/reference/cstdio/scanf/

Search for “Whitespace character: the function will read” and then for “An optional starting asterisk indicates” at this page to clear your doubts.

@anton_lunyov: thanks for your tip, good point :wink: I just said I prefer no so strict judge, but I’m always sticking with format specified in statement, because I never know if the judge is or is not strict :wink:

hm, now I realized, that %d%*c is probably a typo, @anton_lunyov, can you please confirm that?

Why do you think so?
scanf("%d%*c",&T) will exactly input T and skip the next character after T.

silly mistake there @amitupadhyay in the for loop, @anton_lunyov pointed it right. Really, one ignore these things in a tight time bound competition.