-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOldUralLegend.java
93 lines (65 loc) · 2.35 KB
/
OldUralLegend.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class OldUralLegend
{
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
char input[] = in.next().toCharArray();
int cumulativeSum[] = new int[input.length];
cumulativeSum[0] = input[0] - '0';
Map<Integer, List<Integer>> char2pos = new HashMap<>();
char2pos.put(cumulativeSum[0], new ArrayList<>());
char2pos.get(cumulativeSum[0]).add(0);
for (int i = 1; i < input.length; i++) {
int val = input[i] - '0';
cumulativeSum[i] = cumulativeSum[i-1]+val;
if (!char2pos.containsKey(val)) {
char2pos.put(val, new ArrayList<>());
}
char2pos.get(val).add(i);
}
int length = 1;
int dl = 10;
for (int i = 1; i < Integer.MAX_VALUE; i++) {
if (i % dl == 0) {
length++;
dl *= 10;
}
boolean found = false;
int firstDigit = i / (dl/10);
// for (int j = 0; j < input.length - length + 1; j++) {
if ( char2pos.containsKey(firstDigit)) {
for (int j : char2pos.get(firstDigit)) {
if (j >= input.length - length + 1)
continue;
int contains = cumulativeSum[j+length-1];
if (j > 0)
contains -= cumulativeSum[j-1];
if (contains <= i) {
found = compare(i, input, j, length );
// System.out.println("check "+i+" = "+found+" pos="+j+" l="+length);
if (found)
break;
}
}
}
if (!found) {
System.out.println(""+i);
return;
}
}
System.out.println("-1");
}
public static boolean compare(int k, char[] src, int pos, int len) {
while(len > 0) {
int v = k % 10;
k /= 10;
if (v != src[pos+len-1] - '0')
return false;
len--;
}
return true;
}
}