package com university import java io import java util ArrayList impor

 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
package com.university;
import java.io.*;
import java.util.ArrayList;
import java.util.regex.*;
public class Main {
public static void init() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("./src/com/university/input.txt"));
String line;
while ((line = reader.readLine()) != null) {
grammars.add(line.split("\\::="));
}
}
public static void gram(String str) {
Pattern pattern = Pattern.compile("([0-9a-z]+)([A-Z]+)([0-9a-z]+)$");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
if (Character.isUpperCase(matcher.group(i).toCharArray()[0])) {
for (char ch : matcher.group(i).toCharArray())
results.add("<1, " + ch + ">");
} else {
results.add("<0, " + matcher.group(i) + ">");
}
}
}
}
public static void main(String[] args) throws IOException {
init();
for (String[] str : grammars) {
gram(str[0]);
gram(str[1]);
}
for (String str : results)
System.out.println(str);
}
private static ArrayList<String[]> grammars = new ArrayList<String[]>();
private static ArrayList<String> results = new ArrayList<String>();
}