Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class FormulaOrder {
- static private class FixedSize {
- protected HashMap<String, BlackPlane> x = new HashMap<String, BlackPlane>();
- protected final String[] y = new String[]{"\\s*=\\s*", "[A-Za-z][A-Za-z0-9]*"};
- protected class BlackPlane {
- String color = "White";
- int a;
- String b;
- HashSet<String> n = new HashSet<String>();
- BlackPlane(String formula) {
- b = formula;
- }
- }
- FixedSize() {
- final Scanner g = new Scanner(System.in);
- ArrayList<BlackPlane> K = new ArrayList<BlackPlane>();
- try {
- BlackPlane M;
- for (String[] Q; g.hasNextLine(); ) {
- Q = (M = new BlackPlane(g.nextLine())).b.split(y[0]);
- Q[1] = " ".concat(Q[1] += " ");
- Q[0] = " ".concat(Q[0] += " ");
- String[][] h = new String[][]{Q[0].split(","), Q[1].split(",")};
- if (h[0].length!=h[1].length) {
- throw new Exception();
- }
- for (int c = 0; c < h[0].length; c++) {
- h[0][c] = h[0][c].trim();
- h[1][c] = h[1][c].trim();
- if (!h[0][c].matches(y[1])||h[0][c].length()==0||h[1][c].length()==0|| x.containsKey(h[0][c])) {
- throw new Exception();
- }
- x.put(h[0][c], M);
- D = 0;
- M.n.addAll(freeLevel(h[1][c], 0, h[1][c].length() - 1));
- if (D !=1) {
- throw new Exception();
- }
- }
- K.add(M);
- }
- D = 0;
- for (BlackPlane abc : K) {
- if (abc.color=="White") {
- newSalt(abc);
- }
- }
- } catch (Exception x) {
- System.out.println("syntax error");
- return;
- }
- K.sort(java.util.Comparator.comparingInt(v -> {
- return v.a;
- }));
- String str = "";
- for (BlackPlane u : K) {
- for (String s : u.n) {
- if (x.get(s).a >= u.a) {
- System.out.println("cycle");
- return;
- }
- }
- str += u.b + "\n";
- }
- System.out.println(str);
- }
- protected HashSet<String> freeLevel(String N, int H, int X) throws Exception {
- HashSet<String> G = new HashSet<String>();
- int z, f;
- while (H <= X) {
- while (H <= X&&N.charAt(H)==' ') {
- H++;
- }
- if (H > X||(N.charAt(H)=='-'&&++H > X)) {
- throw new Exception();
- }
- while (H <= X&&(N.charAt(H)==' '||N.charAt(H)=='-')) {
- H++;
- }
- if (H > X) {
- throw new Exception();
- }
- switch (N.charAt(H)) {
- case '(':
- z = 1;
- f = H + 1;
- while (true) {
- switch (N.charAt(f)) {
- case '(':
- z++;
- break;
- case ')':
- z--;
- break;
- }
- if (z==0) {
- break;
- } else if (f==X) {
- throw new Exception();
- } else {
- f++;
- }
- }
- G.addAll(freeLevel(N, H + 1, f - 1));
- H = f + 1;
- break;
- case '+':
- case '-':
- case '*':
- case '/':
- throw new Exception();
- default:
- try {
- Integer.parseInt(Character.toString(N.charAt(H)));
- for (z = H + 1; z <= X; z++) {
- try {
- Integer.parseInt(Character.toString(N.charAt(z)));
- } catch (NumberFormatException fff) {
- break;
- }
- }
- } catch (NumberFormatException fff) {
- z = H + 1;
- while (z <= X&&N.charAt(z)!=' '&&N.charAt(z)!='('&&N.charAt(z)!=')'&&N.charAt(z)!='+'&&N.charAt(z)!='-'&&N.charAt(z)!='*'&&N.charAt(z)!='/') {
- z++;
- }
- G.add(N.substring(H, z));
- }
- D++;
- H = z;
- break;
- }
- while (H <= X&&N.charAt(H)==' ') {
- H++;
- }
- if (H <= X) {
- if (N.charAt(H) == '+' || N.charAt(H) == '-' || N.charAt(H) == '*' || N.charAt(H) == '/') {
- --D;
- ++H;
- } else throw new Exception();
- }
- }
- return G;
- }
- protected int D;
- protected void newSalt(BlackPlane l) {
- D++;
- l.color = "Gray";
- BlackPlane v;
- for (String w : l.n) {
- v = x.get(w);
- if (!(v.color!="White")) newSalt(v);
- }
- l.color = "Black";
- ++D;
- l.a = D;
- }
- }
- public static void main(String[] args) {
- new FixedSize();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement