Weird result when using map in June14 question "Forgot Password"

For this question, I first used a map<char,char> for doing the replacement according to the rules.

map<char,char> m;
m[ci] = pi;

Did not work. After a lot of struggling, I finally thought of doing asserts to check whether the final string I’m getting is actually a decimal number. So I counted number of dots ‘.’ and that assert failed.

assert(dots <= 1);

( http://www.codechef.com/viewsolution/4121506 )

I couldn’t get AC, so I thought of just using a char array to do the same mapping from ci -> pi

char m[1000];
memset(m,' ',1000);
m[ci] = pi;

( http://www.codechef.com/viewsolution/4121511 )

Surprisingly this worked. Those two solutions differ at only 3 lines ( line #13, #18, #30). Can someone tell me what’s wrong with the map<char,char> solution?

You have to clear the map before the beginning of each test case otherwise it will store the previous mappings also…There was your fault!!
You can clear the map using m.clear()…

2 Likes

Of course! How did I miss that :smiley: anyways Thanks!