Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5.  
  6. public class FormulaOrder {
  7.  
  8.     static private class FixedSize {
  9.  
  10.         protected HashMap<String, BlackPlane> x = new HashMap<String, BlackPlane>();
  11.         protected final String[] y = new String[]{"\\s*=\\s*", "[A-Za-z][A-Za-z0-9]*"};
  12.  
  13.         protected class BlackPlane {
  14.  
  15.             String color = "White";
  16.             int a;
  17.             String b;
  18.             HashSet<String> n = new HashSet<String>();
  19.  
  20.             BlackPlane(String formula) {
  21.                 b = formula;
  22.             }
  23.  
  24.         }
  25.  
  26.         FixedSize() {
  27.             final Scanner g = new Scanner(System.in);
  28.             ArrayList<BlackPlane> K = new ArrayList<BlackPlane>();
  29.             try {
  30.                 BlackPlane M;
  31.                 for (String[] Q; g.hasNextLine(); ) {
  32.                     Q = (M = new BlackPlane(g.nextLine())).b.split(y[0]);
  33.                     Q[1] = " ".concat(Q[1] += " ");
  34.                     Q[0] = " ".concat(Q[0] += " ");
  35.                     String[][] h = new String[][]{Q[0].split(","), Q[1].split(",")};
  36.                     if (h[0].length!=h[1].length) {
  37.                         throw new Exception();
  38.                     }
  39.                     for (int c = 0; c < h[0].length; c++) {
  40.                         h[0][c] = h[0][c].trim();
  41.                         h[1][c] = h[1][c].trim();
  42.                         if (!h[0][c].matches(y[1])||h[0][c].length()==0||h[1][c].length()==0|| x.containsKey(h[0][c])) {
  43.                             throw new Exception();
  44.                         }
  45.                         x.put(h[0][c], M);
  46.                         D = 0;
  47.                         M.n.addAll(freeLevel(h[1][c], 0, h[1][c].length() - 1));
  48.                         if (D !=1) {
  49.                             throw new Exception();
  50.                         }
  51.                     }
  52.                     K.add(M);
  53.                 }
  54.                 D = 0;
  55.                 for (BlackPlane abc : K) {
  56.                     if (abc.color=="White") {
  57.                         newSalt(abc);
  58.                     }
  59.                 }
  60.             } catch (Exception x) {
  61.                 System.out.println("syntax error");
  62.                 return;
  63.             }
  64.             K.sort(java.util.Comparator.comparingInt(v -> {
  65.                 return v.a;
  66.             }));
  67.             String str = "";
  68.             for (BlackPlane u : K) {
  69.                 for (String s : u.n) {
  70.                     if (x.get(s).a >= u.a) {
  71.                         System.out.println("cycle");
  72.                         return;
  73.                     }
  74.                 }
  75.                 str += u.b + "\n";
  76.             }
  77.             System.out.println(str);
  78.         }
  79.  
  80.         protected HashSet<String> freeLevel(String N, int H, int X) throws Exception {
  81.             HashSet<String> G = new HashSet<String>();
  82.             int z, f;
  83.             while (H <= X) {
  84.                 while (H <= X&&N.charAt(H)==' ') {
  85.                     H++;
  86.                 }
  87.                 if (H > X||(N.charAt(H)=='-'&&++H > X)) {
  88.                     throw new Exception();
  89.                 }
  90.                 while (H <= X&&(N.charAt(H)==' '||N.charAt(H)=='-')) {
  91.                     H++;
  92.                 }
  93.                 if (H > X) {
  94.                     throw new Exception();
  95.                 }
  96.                 switch (N.charAt(H)) {
  97.                     case '(':
  98.                         z = 1;
  99.                         f = H + 1;
  100.                         while (true) {
  101.                             switch (N.charAt(f)) {
  102.                                 case '(':
  103.                                     z++;
  104.                                     break;
  105.                                 case ')':
  106.                                     z--;
  107.                                     break;
  108.                             }
  109.                             if (z==0) {
  110.                                 break;
  111.                             } else if (f==X) {
  112.                                 throw new Exception();
  113.                             } else {
  114.                                 f++;
  115.                             }
  116.                         }
  117.                         G.addAll(freeLevel(N, H + 1, f - 1));
  118.                         H = f + 1;
  119.                         break;
  120.                     case '+':
  121.                     case '-':
  122.                     case '*':
  123.                     case '/':
  124.                         throw new Exception();
  125.                     default:
  126.                         try {
  127.                             Integer.parseInt(Character.toString(N.charAt(H)));
  128.                             for (z = H + 1; z <= X; z++) {
  129.                                 try {
  130.                                     Integer.parseInt(Character.toString(N.charAt(z)));
  131.                                 } catch (NumberFormatException fff) {
  132.                                     break;
  133.                                 }
  134.                             }
  135.                         } catch (NumberFormatException fff) {
  136.                             z = H + 1;
  137.                             while (z <= X&&N.charAt(z)!=' '&&N.charAt(z)!='('&&N.charAt(z)!=')'&&N.charAt(z)!='+'&&N.charAt(z)!='-'&&N.charAt(z)!='*'&&N.charAt(z)!='/') {
  138.                                 z++;
  139.                             }
  140.                             G.add(N.substring(H, z));
  141.                         }
  142.                         D++;
  143.                         H = z;
  144.                         break;
  145.                 }
  146.                 while (H <= X&&N.charAt(H)==' ') {
  147.                     H++;
  148.                 }
  149.                 if (H <= X) {
  150.                     if (N.charAt(H) == '+' || N.charAt(H) == '-' || N.charAt(H) == '*' || N.charAt(H) == '/') {
  151.                         --D;
  152.                         ++H;
  153.                     } else throw new Exception();
  154.                 }
  155.             }
  156.             return G;
  157.         }
  158.  
  159.         protected int D;
  160.         protected void newSalt(BlackPlane l) {
  161.             D++;
  162.             l.color = "Gray";
  163.             BlackPlane v;
  164.             for (String w : l.n) {
  165.                 v = x.get(w);
  166.                 if (!(v.color!="White")) newSalt(v);
  167.             }
  168.             l.color = "Black";
  169.             ++D;
  170.             l.a = D;
  171.         }
  172.  
  173.     }
  174.  
  175.     public static void main(String[] args) {
  176.         new FixedSize();
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement