String Manipulation Problem

Problem : https://www.hackerearth.com/problem/algorithm/free-walk-0f675f40-0d59a400/description/

My solution : https://ide.codingblocks.com/s/327215

Why I am getting 0 for input
1
AAAA?C

The input method is wrong.

You need to flush the input stream before using getline(), other wise it takes the newline character leftover from previous input.

Do this by adding

cin.ignore();

before getline().