This blog is a snippet for my reference. I have many such snippets. Cheers.
- Basic I/O setup.
#incldue<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
}
- Data Structures.
a. vector -> Slice
vector<int> v = {1, 2, 3};
v.push_back(4);
v := []int {1, 2, 3}
v = append(v, 4)
b. Set -> Map[Type]struct{}
#include<set>
set<int> s;
s.insert(1);
s.insert(2);
if(s.find(1) != s.end()) {
// 1 is in set
}
package main
type void struct{}
var member void
s := make(map[int]void)
s[1] = member
s[2] = member
if _, exists := s[1]; exists {
// 1 is in set.
}
- Map -> map
#include<map>
map<string, int> m;
m["string"] = 42;
if(m.find("string") != m.end()) {
cout << m["string"]
}
m := make(map[string]int)
m["key"] = 42
if value, exists := m["key"]; exists {
fmt.Println(value)
}
- Priority Queue -> container/heap
#include<queue>
priority_queue<int> pq;
pq.push(3);
pq.push(4);
while(!pq.empty()) {
cout << pq.top() << "";
pq.pop();
}
Create a type, implement Len, Less, Swap.
import (
"container/heap"
"fmt"
)
type IntHeap []int
func (h IntHeap) Len() int {return len(h)}
func (h IntHeap) Less(i, j int) bool {return h[i] > h[j]}
func (h IntHeap) Swap(i, j int) {h[i], h[j] = h[j], h[i]}
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0: n-1]
return x
}
func main() {
h := &IntHeap{3, 1, 4}
heap.Init(h)
heap.Push(h, 2)
for h.Len() > 0 {
fmt.Println("%d", heap.Pop(h))
}
}
- Stack -> Slice.
#include<stack>
stack<int> s;
s.push(1);
s.push(2);
while(!s.empty()) {
cout << s.top() << "\n";
s.pop();
}
s := []int{}
s = append(s, 1)
s = append(s, 2)
for len(s) > 0 {
fmt.Println("%d", s[len(s)-1])
s = s[:len(s) - 1]
}
- Queue -> slice
#include<queue>
queue<int> q;
q.push(1);
q.push(2);
while(!q.empty()) {
cout << q.front() << " ";
q.pop();
}
package main
q := []int{}
q = append(q, 1)
q = append(q, 2)
for len(q) > 0 {
fmt.Println("%d", q[0])
q = q[1:]
}
- Pair -> struct
#include<utility>
pair<string, int> p = make_pair("one", 1);
cout << p.first << " " << p.second;
type Pair struct {
First int
Second string
}
p := Pair { 1, "one"}
fmt.Println(p.First, p.Second)
- Linked list -> container/list
#include<list>
list<int> l;
l.push_back(1);
l.push_front(2);
for(auto it = l.begin(); it != l.end(); ++it) {
cout << *it << " ";
}
package main
import (
"fmt"
"container/list"
)
l := list.New()
l.PushBack(1)
l.PushFront(2)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Printf("%v", e.Value)
}
- Complete I/O for a sample t, n, m input and a comma separated integers input.
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
string arrStr;
cin.ignore(); // Clear the newline c.
getline(cin, arrStr);
vector<int> arr;
stringstream ss(arrStr);
string numStr;
while(getline(ss, numStr, ',')) {
arr.push_back(stoi(numStr));
}
// Your solution logic here.
// For this example, we'll just print the inputs.
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) {
solve();
}
return 0;
}
package main
import(
"fmt"
"bufio"
"os"
"strconv"
"strings"
)
var scanner *bufio.Scanner
var writer *bufio.Writer
func init() {
scanner = bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
writer = bufio.NewWriter(os.Stdout)
}
func scanInt() int {
scanner.Scan()
num, _ := strconv.Atoi(scanner.Text())
return num
}
func scanInts(n int) []int {
nums := make([]int, n)
for i:= 0; i < n; i++ {
nums[i] = scanInt()
}
return nums
}
func scanLine() string {
scanner.Scan()
return scanner.Text()
}
func solve() {
// Read two integers.
n, m := scanInt(), scanInt()
// Read comma separated integers.
arrStr := scanLine()
strNums := strings.Split(arrStr, ",")
arr := make([]int, len(strNums))
for i, numStr := range strNums {
arr[i], _ = strconv.Atoi(numStr)
}
}
func scanInts() []int {
scanner.Scan()
return scanner.Text()
}
func main() {
defer writer.Flush()
t := scanInt()
for i := 0; i<t; i++ {
solve()
}
}