Last active
June 2, 2020 10:03
-
-
Save p1scescom/a76187d6cb38f41a7d41c4d7baeeadd4 to your computer and use it in GitHub Desktop.
Competitive programming template for julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main() | |
end | |
import Base.parse | |
import Base.StringVector | |
parse(::Type{String}, str::AbstractString) = str | |
isdelim(x::UInt8, xs::Set{UInt8}) = x in xs | |
const delimset = Set([0x0a, 0x20]) | |
function myreaduntil(s::IO, delims::Set{UInt8})::Vector{UInt8} | |
out = StringVector(0) | |
c::UInt8 = 0x00 | |
while !eof(s) | |
c = read(s, UInt8) | |
!isdelim(c, delims) && break | |
end | |
push!(out, c) | |
eof(s) && return out | |
while !eof(s) | |
c = read(s, UInt8) | |
isdelim(c, delims) && break | |
push!(out, c) | |
end | |
return out | |
end | |
function readword(io::IO = stdin, delims = delimset)::String | |
word = myreaduntil(io, delims) | |
i = length(word) | |
if i == 0 || word[i] != 0x0a | |
return String(word) | |
elseif i < 2 || word[i-1] != 0x0d | |
return String(resize!(word,i-1)) | |
else | |
return String(resize!(word,i-2)) | |
end | |
end | |
pread(ty) = parse(ty, readword()) | |
reads(tys...)::Tuple{tys...} = Tuple{tys...}(pread(ty) for ty in tys) | |
function readvec(tys::Tuple , len::Signed)::Tuple{map(x -> Vector{x},tys)...} | |
rv = Tuple{map(x -> Vector{x},tys)...}(Vector{ty}(undef,len) for ty in tys) | |
for i in 1:len | |
for j in 1:length(tys) | |
@inbounds rv[j][i] = pread(tys[j]) | |
end | |
end | |
rv | |
end | |
readvec(ty::Type, len::Signed)::Vector{ty} = @inbounds ty[pread(ty) for i in 1:len] | |
function readmat(ty::Type, s...)::Matrix{ty} | |
v = Matrix{ty}(undef, s...) | |
@inbounds for i in 1:s[1] | |
v[i,:] = readvec(ty, s[2]) | |
end | |
v | |
end | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright 2020 p1scescom | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment