sapjv's blog

By sapjv, history, 6 weeks ago, In English

I was trying to solve a problem, but I encountered a weird error. I don't know why my code is giving me wrong output. Can someone help me what is wrong in my code ? Thanks in advance.

Code

If I am giving this input :

1 2
abc
cdf
edf

then my code is giving me this output : len1 = 2 , len2 = 2 ( while len1 must be 1 )

Note : If I am removing that common string checking part — these two lines ( line1 and line2 ) from 2nd for loop, then it is giving me correct output as len1 = 1 , len2 = 2

Please help me.

Tags c++
 
 
 
 
  • Vote: I like it
  • -13
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Do you want to say something about that note which i have specified here ?

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it +9 Vote: I do not like it

      Yeah as scopeInfinity said mp1[s]!=0 adds another entry in the mp1 if s is not present already. Hence your len1 is 2 (For the given case).

      If you want to check if a string s is present map s or not then use mp1.find(s)!=mp1.end() Because map::find returns an iterator to the element if present otherwise its equal to mp1.end()

»
6 weeks ago, # |
  Vote: I like it -51 Vote: I do not like it

Errichto , pikmike , Ashishgup can you please help me with this issue ? Thanks in advance.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Did you have to ping us after getting help from others already? Downvote.