A problem I tried to solve today was Games of Strings
. A quite simple problem but when I tried to implement my solution with golang it failed. In fact it gave weird results for the same submission. Some gave WA while some gave TLE!
These are the sources in cpp and golang,
The cpp one:
code spoiler
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
auto strings = set<string>();
for (int x = 0; x < n; x++) {
string input;
cin >> input;
strings.insert(input);
}
int k;
cin >> k;
auto it = strings.begin();
for (int x = 0; x < k; x++) {
cout << *it;
it++;
if (it == strings.end())
break;
}
cout << '\n';
return 0;
}
And the go code:
code spoiler
package main
import (
"fmt"
"sort"
)
func main() {
var n int
fmt.Scanf("%d", &n)
list := make([]string, n)
for i := 0; i < n; i++ {
fmt.Scanf("%s", &list[i])
}
sort.Strings(list)
var k int
fmt.Scanf("%d", &k)
for idx := 0; idx < k; idx++ {
if idx == len(list) {
break
}
fmt.Print(list[idx])
}
fmt.Println()
}
The solutions are virtually the same and gives same result in my local environment. However toph seems to not like golang that much.