From 692189eefb2f9bae62e23ca8d7a1553e447af3da Mon Sep 17 00:00:00 2001 From: Andrei Ovcharenko Date: Fri, 31 Jul 2026 17:43:36 +0300 Subject: [PATCH] Validate sprint7 solver input bounds --- .../java/algorithms/sprint7/EqualSums.java | 27 ++++++++++++++++--- .../sprint7/LevenshteinDistance.java | 17 ++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/algorithms/sprint7/EqualSums.java b/src/main/java/algorithms/sprint7/EqualSums.java index 0ab867e..f6f82c3 100644 --- a/src/main/java/algorithms/sprint7/EqualSums.java +++ b/src/main/java/algorithms/sprint7/EqualSums.java @@ -31,18 +31,30 @@ Пространственная сложность — O(S / 64). */ public class EqualSums { + private static final int MAX_GAMES = 300; + private static final int MAX_TOTAL = MAX_GAMES * 300; static boolean solve(int[] points) { - int total = 0; + if (points.length > MAX_GAMES) { + throw new IllegalArgumentException("Too many games"); + } + + long total = 0; for (int point : points) { + if (point < 0) { + throw new IllegalArgumentException("Point value is out of range"); + } total += point; + if (total > MAX_TOTAL) { + throw new IllegalArgumentException("Total points are out of range"); + } } - if ((total & 1) == 1) { + if ((total & 1L) == 1L) { return false; } - int target = total / 2; + int target = (int) (total / 2); long[] reachable = new long[(target >> 6) + 1]; reachable[0] = 1L; @@ -168,10 +180,17 @@ private static void run() throws Exception { FastOut out = new FastOut(System.out); int n = in.nextInt(); + if (n < 0 || n > MAX_GAMES) { + throw new IOException("Number of games is out of range"); + } int[] points = new int[n]; for (int i = 0; i < n; i++) { - points[i] = in.nextInt(); + int point = in.nextInt(); + if (point < 0) { + throw new IOException("Point value is out of range"); + } + points[i] = point; } out.writeAscii(solve(points) ? "True" : "False"); diff --git a/src/main/java/algorithms/sprint7/LevenshteinDistance.java b/src/main/java/algorithms/sprint7/LevenshteinDistance.java index 482e1fb..2547124 100644 --- a/src/main/java/algorithms/sprint7/LevenshteinDistance.java +++ b/src/main/java/algorithms/sprint7/LevenshteinDistance.java @@ -27,8 +27,12 @@ Пространственная сложность: O(min(n, m)), потому что хранится только две строки динамики. */ public class LevenshteinDistance { + private static final int MAX_LINE_LENGTH = 1000; static int solve(String first, String second) { + if (first.length() > MAX_LINE_LENGTH || second.length() > MAX_LINE_LENGTH) { + throw new IllegalArgumentException("Input line is too long"); + } String s = first; String t = second; @@ -94,8 +98,8 @@ private int read() throws IOException { return buf[ptr++]; } - String nextLine() throws IOException { - byte[] tmp = new byte[1024]; + String nextLine(int maxLength) throws IOException { + byte[] tmp = new byte[Math.min(1024, maxLength + 1)]; int size = 0; int c = read(); @@ -105,8 +109,11 @@ String nextLine() throws IOException { while (c != -1 && c != '\n') { if (c != '\r') { + if (size == maxLength) { + throw new IOException("Input line is too long"); + } if (size == tmp.length) { - byte[] grown = new byte[tmp.length * 2]; + byte[] grown = new byte[Math.min(tmp.length * 2, maxLength)]; System.arraycopy(tmp, 0, grown, 0, tmp.length); tmp = grown; } @@ -169,8 +176,8 @@ private static void run() throws Exception { FastIn in = new FastIn(System.in); FastOut out = new FastOut(System.out); - String s = in.nextLine(); - String t = in.nextLine(); + String s = in.nextLine(MAX_LINE_LENGTH); + String t = in.nextLine(MAX_LINE_LENGTH); out.writeInt(solve(s, t)); out.writeByte('\n');