Approach
Depth-first search
For CCC 1999 P5 - Letter Arithmetic, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 184 lines of Java from the credited upstream file ccc99s5.java.
- The implementation visibly relies on sequence storage.
- 11 loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 4567 8910 111213 141516 17import java.awt.*;18import hsa.*;19 20public class P5LetterArithmetic21{22 static Console cc;23 static public int [] a = new int [10]; 24 static public String [] s = new String [3]; 25 static public String nodups;26 static public int cn = 9; 27 static public int pn; 28 static public byte [] value = new byte [255]; 29 static public boolean stop;30 static public TextInputFile fi = new TextInputFile ("letter.in");31 static public TextOutputFile fo = new TextOutputFile ("letter.out");32 33 public static void main (String [] args)34 {35 cc = new Console ();36 int count;37 38 count = fi.readInt ();39 for (int i = 1 ; i <= count ; i++)40 {41 s [0] = fi.readString ();42 s [1] = fi.readString ();43 s [2] = fi.readString ();44 stop = false;45 nodups = findUnique (s);46 pn = nodups.length () - 1; 47 doit (s, nodups);48 }49 }50 51 52 53 public static String findUnique (String [] s)54 {55 String u = new String ();56 int k;57 u = "";58 for (int j = 0 ; j < 3 ; j++)59 for (int i = 0 ; i < s [j].length () ; i++)60 {61 k = 0;62 while (k < u.length () && u.charAt (k) != s [j].charAt (i))63 k++;64 if (k >= u.length ())65 u = u + s [j].charAt (i);66 }67 return u;68 }69 70 71 public static void doit (String [] s, String u)72 {73 74 75 76 choose (0, pn);77 78 }79 80 81 82 83 public static void choose (int b, int c)84 {85 if (c == -1)86 permute (0);87 else if (!stop)88 for (int i = b ; i < cn - c + 1 ; i++)89 {90 a [c] = i;91 choose (i + 1, c - 1);92 }93 }94 95 96 97 98 public static void permute (int i)99 {100 int t;101 if (i > pn)102 process ();103 else if (!stop)104 {105 permute (i + 1);106 for (int j = i + 1 ; j <= pn ; j++)107 {108 t = a [j];109 a [j] = a [i];110 a [i] = t;111 permute (i + 1);112 t = a [j];113 a [j] = a [i];114 a [i] = t;115 }116 }117 }118 119 120 public static void process ()121 {122 123 for (int i = 0 ; i < nodups.length () ; i++)124 value [nodups.charAt (i)] = (byte) a [i];125 126 127 128 if (okay ())129 {130 for (byte j = 0 ; j < s [0].length () ; j++)131 {132 cc.print (value [s [0].charAt (j)]);133 fo.print (value [s [0].charAt (j)]);134 }135 cc.println ();136 fo.println ();137 for (byte j = 0 ; j < s [1].length () ; j++)138 {139 cc.print (value [s [1].charAt (j)]);140 fo.print (value [s [1].charAt (j)]);141 }142 cc.println ("");143 fo.println ();144 for (byte j = 0 ; j < s [2].length () ; j++)145 {146 cc.print (value [s [2].charAt (j)]);147 fo.print (value [s [2].charAt (j)]);148 }149 cc.println ("");150 fo.println ();151 cc.println ("");152 fo.println ();153 stop = true;154 }155 }156 157 158 159 160 public static boolean okay ()161 {162 int carry = 0;163 int t;164 int j = s [0].length () - 1;165 int k = s [1].length () - 1;166 int i = s [2].length () - 1;167 boolean fine = true;168 while (i >= 0 && fine)169 {170 t = carry;171 if (j >= 0)172 t = t + value [s [0].charAt (j--)];173 if (k >= 0)174 t = t + value [s [1].charAt (k--)];175 carry = t / 10;176 t = t % 10;177 fine = t == value [s [2].charAt (i--)];178 }179 return fine && carry == 0;180 }181}182 183 184