-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.java
More file actions
285 lines (268 loc) · 9.53 KB
/
task1.java
File metadata and controls
285 lines (268 loc) · 9.53 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* this file is part of bp-json-xml project.
*
* Copyright (C) 2025 Hesam Tavakoli
*
* bp-json-xml is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
public class task1 {
private static abstract class json_node {
abstract int count();
}
private static class json_primitive extends json_node {
Object value;
json_primitive(Object value) {
this.value = value;
}
@Override
int count() {
return 1;
}
}
private static class json_array_node extends json_node {
List<json_node> elements = new ArrayList<>();
@Override
int count() {
int total = 1;
for(json_node node : elements){
total += node.count();
}
return total;
}
}
private static class json_object_node extends json_node {
Map<String, json_node> members = new HashMap<>();
@Override
int count() {
int total = 1;
for(json_node node : members.values()){
total += node.count();
}
return total;
}
}
private static class json_parser {
private String s;
private int index;
json_parser(String s) {
this.s = s;
this.index = 0;
}
private void skip_whitespace() {
while(index < s.length() && Character.isWhitespace(s.charAt(index))){
index++;
}
}
private char current_char() throws Exception {
if(index >= s.length()){
throw new Exception("unexpected end of input");
}
return s.charAt(index);
}
private void expect_char(char expected) throws Exception {
skip_whitespace();
if(index >= s.length() || s.charAt(index) != expected){
throw new Exception("expected '" + expected + "' at position " + index);
}
index++;
}
json_node parse_value() throws Exception {
skip_whitespace();
if(index >= s.length()){
throw new Exception("empty input");
}
char c = s.charAt(index);
if(c == '{'){
return parse_object();
} else if(c == '['){
return parse_array();
} else if(c == '"'){
return new json_primitive(parse_string());
} else if(c == 't' || c == 'f' || c == 'n'){
return new json_primitive(parse_literal());
} else if(c == '-' || c == '.' || Character.isDigit(c)){
return new json_primitive(parse_number());
} else {
throw new Exception("invalid character at position " + index + ": " + c);
}
}
private json_node parse_object() throws Exception {
expect_char('{');
skip_whitespace();
json_object_node obj = new json_object_node();
if(index < s.length() && s.charAt(index) == '}'){
index++;
return obj;
}
while(true){
skip_whitespace();
if(index >= s.length() || s.charAt(index) != '"'){
throw new Exception("expected string key at position " + index);
}
String key = parse_string();
skip_whitespace();
expect_char(':');
json_node value = parse_value();
obj.members.put(key, value);
skip_whitespace();
if(index < s.length() && s.charAt(index) == ','){
index++;
} else {
break;
}
}
skip_whitespace();
expect_char('}');
return obj;
}
private json_node parse_array() throws Exception {
expect_char('[');
skip_whitespace();
json_array_node array = new json_array_node();
if(index < s.length() && s.charAt(index) == ']'){
index++;
return array;
}
while(true){
json_node element = parse_value();
array.elements.add(element);
skip_whitespace();
if(index < s.length() && s.charAt(index) == ','){
index++;
} else {
break;
}
}
skip_whitespace();
expect_char(']');
return array;
}
private String parse_string() throws Exception {
expect_char('"');
StringBuilder sb = new StringBuilder();
while(index < s.length()){
char c = s.charAt(index++);
if(c == '"'){
return sb.toString();
} else if(c == '\\'){
if(index >= s.length()){
throw new Exception("unexpected end of input in string escape");
}
char next = s.charAt(index++);
if(next == '"' || next == '\\'){
sb.append(next);
} else {
throw new Exception("invalid escape character: \\" + next);
}
} else if(c == '\n' || c == '\r'){
throw new Exception("newline in string not allowed");
} else {
if(c >= 32 && c <= 126){
sb.append(c);
} else {
throw new Exception("invalid character in string: " + c);
}
}
}
throw new Exception("unterminated string");
}
private Number parse_number() throws Exception {
skip_whitespace();
int start = index;
boolean negative = false;
if(index < s.length() && s.charAt(index) == '-'){
negative = true;
index++;
}
boolean has_digit = false;
boolean has_dot = false;
if(index < s.length() && s.charAt(index) == '.'){
has_dot = true;
index++;
}
while(index < s.length() && Character.isDigit(s.charAt(index))){
has_digit = true;
index++;
}
if(index < s.length() && s.charAt(index) == '.'){
if(has_dot){
throw new Exception("multiple dots in number at position " + index);
}
has_dot = true;
index++;
if(index >= s.length() || !Character.isDigit(s.charAt(index))){
throw new Exception("expected digit after dot at position " + index);
}
while(index < s.length() && Character.isDigit(s.charAt(index))){
has_digit = true;
index++;
}
}
if(!has_digit){
throw new Exception("invalid number format at position " + start);
}
String num_str = s.substring(start, index);
try {
if(has_dot){
return Double.parseDouble(num_str);
} else {
return Long.parseLong(num_str);
}
} catch (NumberFormatException e) {
throw new Exception("number format error: " + num_str);
}
}
private Object parse_literal() throws Exception {
skip_whitespace();
if(s.startsWith("true", index)){
index += 4;
return Boolean.TRUE;
} else if(s.startsWith("false", index)){
index += 5;
return Boolean.FALSE;
} else if(s.startsWith("null", index)){
index += 4;
return null;
} else {
throw new Exception("invalid literal at position " + index);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
json_parser parser = new json_parser(input);
json_node root = null;
try {
root = parser.parse_value();
parser.skip_whitespace();
if(parser.index != input.length()){
System.out.println(0);
return;
}
} catch (Exception e) {
System.out.println(0);
return;
}
if(!(root instanceof json_object_node)){
System.out.println(0);
} else {
System.out.println(root.count());
}
}
}