joanlopez's blog

By joanlopez, history, 6 days ago, In English,


I'm solving some of the problems to practise with and learn Go language. The problem is that I can't solve those problems where you initially have an integer that means how many times you will have to solve the problem itself.

I mean problems like 4C - Registration System or 71A - Way Too Long Words.

I'm trying the tests in my machine with the Gogland IDE and it works well, the problem is when the code is run by the Codeforces' test runner.

Here you can see my submissions: 31218923 & 31238209

The process I follow to reproduce the tests is introducing the input, line per line, so, for example, introducing the number, then pushing ENTER, then typing the first word and so on.. and I think that the Codeforces' test runner does not proceed this way, however, i have no idea how to deal with it in Go because i'm just starting with this language.

Thanks a lot!


6 days ago, # |
  Vote: I like it +6 Vote: I do not like it

I don't know Go, but my language-agnostic guess would be that fmt.Scanf("%d", &n) leaves the cursor at the end of the first line. So when you then read until a newline character in a loop, word, _ := reader.ReadString('\n'), the first one actually gets the empty suffix of the first line of input. A solution in this case would be to skip that newline, right after reading the first integer (by the same reader.ReadString('\n') or other means), and then proceed solving the test cases in a loop.

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

    Finally I've found a solution for the given problem. There's another way to read the input from the stdin in Go that worked for me:

    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {

    You can see it in my new submissions: 31258982 31258645

    Thanks a lot!