ojudge

A self-hosted competitive programming site written in OCaml
git clone git://git.laack.co/ojudge.git
Log | Files | Refs | README

commit 92b9c51ee0877e2b96207e39bf8db351ad8effb5
parent 141bf96e08257b717e27fb99fe3efc73c1057576
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 15 Nov 2026 13:49:11 -0600

Basic rendering of problems with link to submit page (not started yet)

Diffstat:
MMakefile | 1+
Massets/problems/skyline/problem.html | 42+++++++++++++++++++++++++++++++-----------
Aassets/problems/sum-of-matches/problem.html | 20++++++++++++++++++++
Dassets/problems/sum-of-matches/problem.txt | 16----------------
Mojudge/bin/dune | 5+++++
Aojudge/bin/problem.ml | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 136 insertions(+), 27 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,6 +2,7 @@ ocaml: rm -rf cgi/* cd ojudge && dune build cp ./ojudge/_build/install/default/bin/home cgi/ + cp ./ojudge/_build/install/default/bin/problem cgi/ ensure-config: id http # required for lighttpd diff --git a/assets/problems/skyline/problem.html b/assets/problems/skyline/problem.html @@ -1,38 +1,58 @@ <p> Time limit per test: 2 seconds +</p> +<p> Memory limit per test: 256 megabytes +</p> +<p> There are buildings in front of you with heights [a_0,a_1,...,a_{n-1}], where a_i is an integer. Assume each building has a depth of 100 units, there is a 10 unit gap between each of them, they are all directly in front of you, and the ground is flat. +</p> +<p> The first building is 100 units in front of you, and your eyes are 6 units above ground level. How many buildings are visible? +</p> -Constraints: +<h2>Constraints</h2> -- 10 <= a_i <= 10^6 -- 1 <= n <= 2*10^5 +<ul> +<li> 10 <= a_i <= 10^6 </li> +<li> 1 <= n <= 2*10^5 </li> +</ul> -Inputs: +<h2>Inputs</h2> -- first line: n -- second line: the n integers of a +<ul> +<li> first line: n </li> +<li> second line: the n integers of a </li> +</ul> -Examples: +<h2>Examples</h2> -Input 1: +<h3>Input 1</h3> +<pre> 6 10 47 500 1000 2783 4616 +</pre> -Output 1: +<h3>Output 1</h3> +<pre> 6 +</pre> -Input 2: +<h3>Input 2</h3> +<pre> 8 100 187 100 1000 1250 10000 1238 47100 +</pre> -Output 2: +<h3>Output 2</h3> +<pre> 4 +</pre> + </p> diff --git a/assets/problems/sum-of-matches/problem.html b/assets/problems/sum-of-matches/problem.html @@ -0,0 +1,20 @@ +<p> +Time: 2 seconds +</p> +<p> +Memory: 256 megabytes +</p> + +<p> +Given strings a,b,c,d find the number of substrings of a,b, and c that match d, output the sum. +</p> + +<h2>Constraints</h2> + +<ul> +<li>1 <= len(a) <= 4 * 10^5</li> +<li>1 <= len(b) <= 4 * 10^5</li> +<li>1 <= len(c) <= 4 * 10^5</li> +<li>1 <= len(d) <= 4 * 10^5</li> +<li>there are at most 10^2 matches</li> +</ul> diff --git a/assets/problems/sum-of-matches/problem.txt b/assets/problems/sum-of-matches/problem.txt @@ -1,16 +0,0 @@ -Time: 2 seconds -Memory: 256 megabytes - -Given strings a,b,c,d find the number of substrings of a,b, and c that match d, output the sum. - -Constraints: - -- 1 <= len(a) <= 4 * 10^5 -- 1 <= len(b) <= 4 * 10^5 -- 1 <= len(c) <= 4 * 10^5 -- 1 <= len(d) <= 4 * 10^5 -- there are at most 10^2 matches - ---- - -I'm uncertain about this problem because it seems like floating point precision is enough, but that's both non-obvoius, and I lack the current time / skills to prove that. diff --git a/ojudge/bin/dune b/ojudge/bin/dune @@ -2,3 +2,8 @@ (public_name home) (name home) (libraries ojudge)) + +(executable + (public_name problem) + (name problem) + (libraries ojudge unix)) diff --git a/ojudge/bin/problem.ml b/ojudge/bin/problem.ml @@ -0,0 +1,79 @@ +let printHTMLPreamble () = + print_endline "Content-type: text/html\n\n"; + print_endline "<html>" + +let fileToString filepath = + let chan = open_in filepath in + let rec acc () = + try + let line = input_line chan in + line ^ acc () + with End_of_file -> + close_in chan; "" + in acc () + +let safeFileToString filepath = + match Sys.file_exists filepath with + | true -> Some (fileToString filepath) + | false -> None + +let printHTMLEnding () = + print_endline "</html>" + +let getEntireEnv () = + Unix.environment () + +let matchPrefix input prefix = + if (String.length input) < (String.length prefix) then false + else + if String.sub input 0 (String.length prefix) <> prefix then false else true + +let getEnv varName = + try Some (Unix.getenv varName) + with Not_found -> None + +let printEnv () = + let env = getEntireEnv () in + let rec acc idx = + if idx = (Array.length env) then () + else (print_endline env.(idx); acc (idx+1)) + in acc 0 + +let isAlpha ch = + let num = int_of_char ch in + num >= (int_of_char 'a') && num <= (int_of_char 'z') + +let isAlphaAndSlash input = + (String.fold_left (fun valid ch -> (if ((isAlpha ch || ch = '-') && valid) then true else false)) true input) + +(* pre: The problem name input only contains alpha and '-' characters *) +let printProblem safeProblemName = + let fullPath = "/var/lib/ojudge/problems/" ^ safeProblemName ^ "/problem.html" in + match (safeFileToString fullPath) with + | Some x -> (print_endline x; print_endline ("<a href=\"/submit/" ^ safeProblemName ^ "\">Submit code</a>")) + | None -> print_endline "No such problem" + +let printResponse () = + printHTMLPreamble (); + + let path = (match (getEnv "REQUEST_URI") with + | Some x -> x + | None -> "" + ) in + let problemSize = (String.length "/problem/") in + if matchPrefix path "/problem/" + then let problemNameUnsafe = (String.sub path problemSize ((String.length path)- problemSize)) + in + + (* Problems that are lowercase with '-' are the only supported problem names. *) + (* There's no requirement that the rendered html for a problem contains the problem name, that's only a requirement of the url *) + + if isAlphaAndSlash problemNameUnsafe + then printProblem problemNameUnsafe + else print_endline "This problem doesn't exist" + else + (); + + printHTMLEnding () + +let () = printResponse ()