I was biting a competitive pro a few years ago. I went to light blue for the time being, but after that I didn't do much, and now I have almost nothing to do. I used Ruby to solve most of the problems until it turned light blue. I thought that the library I was using at that time would be useful to someone, so I will explain the library in this article and make a memorial service for the library I was using.

The problems I solved in the past are left at https://github.com/getty104/AtCoder. It may be easier to get an image of how to use it by looking at the code.

I will stick the snippet I used at the beginning as it is. I will explain how to use these libraries and the scenes to use them.

```
require 'prime'
require 'set'
require 'tsort'
include Math
ALP = ('a'..'z').to_a
INF = 0xffffffffffffffff
def max(a,b); a > b ? a : b end
def min(a,b); a < b ? a : b end
def gif; gets.to_i end
def gff; gets.to_f end
def gsf; gets.chomp end
def gi; gets.split.map(&:to_i) end
def gf; gets.split.map(&:to_f) end
def gs; gets.chomp.split.map(&:to_s) end
def gc; gets.chomp.split('') end
def pr(num); num.prime_division end
def pr?(num); Prime.prime?(num) end
def digit(num); num.to_s.length end
def array(s,ini=nil); Array.new(s){ini} end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end
def rep(num); num.times{|i|yield(i)} end
def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end
```

I think that the competition pro will receive the data in question with standard input. Since the input format of the problem has a fixed pattern, I prepared some functions so that it can be implemented quickly. I will explain them

gif
`gif`

is a function that takes input as a single integer.
It can be used when the input format is one integer.

Usage example

`Input format`

```
N
```

`Implementation`

```
n = gif
```

gff
`gff`

is the Float version of` gif. Use when you want to treat the input as a float

`Input example`

```
R
```

`Implementation`

```
r = gff
```

gsf
`gsf`

is a function used when you want to receive input as one character string. This function is often used to treat the received string as a query.
Use `gc`

, which will be described later, to manipulate the received character string.

`Example`

```
S
```

`Implementation`

```
s = gsf
```

gi
This function is used when `gi`

receives multiple integers in one line. I think it's the function I use most often.

`Input example 1`

```
A B
```

`Implementation 1`

```
a, b = gi
```

`Input example 2`

```
N1 N2 ... Nk
```

`Implementation 2`

```
n = gi
```

gf
Float version of `gi`

. It is used when you want to receive multiple values as Float in one line.

`Input example`

```
r1 r2
```

`Implementation`

```
r1, r2 = gf
```

gs
This is the String version of `gi`

. It is used when you want to receive multiple values as a String in one line.

`Input example`

```
S1 S2
```

`Implementation`

```
s1, s2 = gs
```

gc This function can be used when you want to receive a character string as an array (like Char).

`Input example`

```
C
```

`Implementation`

```
c = gc
```

In addition to input and output, there were various functions prepared to reduce the description, so I will introduce them.

max, min
In ruby, there are `max`

and` min`

as array methods, but in AtCoder, binary comparison is often performed, so we prepared the methods `min`

,` max`

.

digit
`digit`

is a function that can get the number of digits of a number.

`Example`

```
digit(100) # => 3
```

pr
`pr`

is a function that factores a given value into prime factors. An alias for `Integer # prime_division`

in` prime`

of the standard Ruby module.

`Example`

```
pr(100) #=> [[2, 2], [5, 2]]
```

pr?
`pr?`

Is a function that determines the prime number of a given value.

`Example`

```
pr?(2) #=> true
```

array, darray
If you are a competition pro, you may need to prepare an array with initial values.
`array`

and` darray`

are functions that can be used in such cases. `array`

can be used when you want to prepare a one-dimensional array, and` darray`

can be used when you want to prepare a two-dimensional array.

`Example`

```
dp = darray(100, 100, INF)
```

rep, repl
I feel that `rep`

and` repl`

are familiar functions when doing competition pros.
This is a function that makes it possible to write a for statement concisely.

`Example`

```
rep 10000 do |i|
#Some processing
end
repl 1, 10 do |i|
#Some processing
end
```

INF, ALP
`INF`

is an alias for an infinite value. It is often used by default.
`ALP`

is an array of alphabets. I use it once in a while.

There are many disadvantages to doing a competitive pro in Ruby in terms of performance and library, but if you have a chance to do a pro in Ruby today, I would appreciate it if you could refer to it.

Recommended Posts