import java.util.ArrayList;
import java.util.List;
public class Komachi {
public static void komachi(int[] lhs, int rhs) {
if (lhs.length == 0) return;
List<Term> expression = new ArrayList<Term>();
expression.add(new TermValue(lhs[0]));
for (int index = 1; index < lhs.length; index++) {
List<Term> work = new ArrayList<Term>(expression);
expression.clear();
for (Term term: work) {
List<Term> list = createTerm(term, lhs[index]);
expression.addAll(list);
};
}
int counter = 1;
for (Term term: expression) {
if (term.getValue() == rhs) {
System.
out.
println(counter
++ + ": " + term.
toString() + " = " + rhs
); }
}
}
private static List<Term> createTerm(Term lhs, int r) {
List<Term> result = new ArrayList<Term>();
Operator[] operators = Operator.values();
for (int index = 0; index < operators.length; index++) {
result.add(new TermData(operators[index], lhs, new TermValue(r)));
}
return result;
}
public enum Operator {
None {
public int getPriority() { return 10; }
public int operate(int lhs, int rhs) { return lhs * 10 + rhs; }
public String toString
() { return ""; } },
Plus {
public int getPriority() { return 1; }
public int operate(int lhs, int rhs) { return lhs + rhs; }
public String toString
() { return "+"; } },
Minus {
public int getPriority() { return 1; }
public int operate(int lhs, int rhs) { return lhs - rhs; }
public String toString
() { return "-"; } },
Times {
public int getPriority() { return 2; }
public int operate(int lhs, int rhs) { return lhs * rhs; }
public String toString
() { return "*"; } };
public abstract int getPriority();
public abstract int operate(int lhs, int rhs);
}
private static interface Term {
public boolean isOperate();
public int getValue();
}
private static class TermValue implements Term {
public final int value;
public TermValue(int val) {
value = val;
}
public boolean isOperate() { return false; }
public int getValue() {
return value;
}
}
private static class TermData implements Term {
public final Operator op;
public final Term lhs;
public final Term rhs;
/**
* 右辺は追加分と考えて、演算子の順序に従ってデータを正規化します。
*/
public TermData(Operator o, Term l, Term r) {
Operator operator = o;
Term lterm = l;
Term rterm = r;
if (l.isOperate()) {
TermData data = (TermData) l;
if (operator.getPriority() > data.op.getPriority()) {
rterm = new TermData(operator, data.rhs, rterm);
lterm = data.lhs;
operator = data.op;
}
}
op = operator;
lhs = lterm;
rhs = rterm;
}
public boolean isOperate() { return true; }
public int getValue() {
return op.operate(lhs.getValue(), rhs.getValue());
}
return lhs.toString() + op.toString() + rhs.toString();
}
}
public static void main
(String[] args
) { long start
= System.
currentTimeMillis(); komachi(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, 100);
long end
= System.
currentTimeMillis(); System.
out.
println("elipse: " + (end
- start
) + "(ms)"); }
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKCnB1YmxpYyBjbGFzcyBLb21hY2hpIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQga29tYWNoaShpbnRbXSBsaHMsIGludCByaHMpIHsKICAgICAgICBpZiAobGhzLmxlbmd0aCA9PSAwKSByZXR1cm47CgogICAgICAgIExpc3Q8VGVybT4gZXhwcmVzc2lvbiA9IG5ldyBBcnJheUxpc3Q8VGVybT4oKTsKICAgICAgICBleHByZXNzaW9uLmFkZChuZXcgVGVybVZhbHVlKGxoc1swXSkpOwogICAgICAgIGZvciAoaW50IGluZGV4ID0gMTsgaW5kZXggPCBsaHMubGVuZ3RoOyBpbmRleCsrKSB7CiAgICAgICAgICAgIExpc3Q8VGVybT4gd29yayA9IG5ldyBBcnJheUxpc3Q8VGVybT4oZXhwcmVzc2lvbik7CiAgICAgICAgICAgIGV4cHJlc3Npb24uY2xlYXIoKTsKICAgICAgICAgICAgZm9yIChUZXJtIHRlcm06IHdvcmspIHsKICAgICAgICAgICAgICAgIExpc3Q8VGVybT4gbGlzdCA9IGNyZWF0ZVRlcm0odGVybSwgbGhzW2luZGV4XSk7CiAgICAgICAgICAgICAgICBleHByZXNzaW9uLmFkZEFsbChsaXN0KTsKICAgICAgICAgICAgfTsKICAgICAgICB9CgogICAgICAgIGludCBjb3VudGVyID0gMTsKICAgICAgICBmb3IgKFRlcm0gdGVybTogZXhwcmVzc2lvbikgewogICAgICAgICAgICBpZiAodGVybS5nZXRWYWx1ZSgpID09IHJocykgewogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGNvdW50ZXIrKyArICI6ICIgKyB0ZXJtLnRvU3RyaW5nKCkgKyAiID0gIiArIHJocyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBwcml2YXRlIHN0YXRpYyBMaXN0PFRlcm0+IGNyZWF0ZVRlcm0oVGVybSBsaHMsIGludCByKSB7CiAgICAgICAgTGlzdDxUZXJtPiByZXN1bHQgPSBuZXcgQXJyYXlMaXN0PFRlcm0+KCk7CiAgICAgICAgT3BlcmF0b3JbXSBvcGVyYXRvcnMgPSBPcGVyYXRvci52YWx1ZXMoKTsKICAgICAgICBmb3IgKGludCBpbmRleCA9IDA7IGluZGV4IDwgb3BlcmF0b3JzLmxlbmd0aDsgaW5kZXgrKykgewogICAgICAgICAgICByZXN1bHQuYWRkKG5ldyBUZXJtRGF0YShvcGVyYXRvcnNbaW5kZXhdLCBsaHMsIG5ldyBUZXJtVmFsdWUocikpKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgIH0KCgogICAgcHVibGljIGVudW0gT3BlcmF0b3IgewogICAgICAgIE5vbmUgewogICAgICAgICAgICBwdWJsaWMgaW50IGdldFByaW9yaXR5KCkgeyByZXR1cm4gMTA7IH0KICAgICAgICAgICAgcHVibGljIGludCBvcGVyYXRlKGludCBsaHMsIGludCByaHMpIHsgcmV0dXJuIGxocyAqIDEwICsgcmhzOyB9CiAgICAgICAgICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKSB7IHJldHVybiAiIjsgfQogICAgICAgIH0sCiAgICAgICAgUGx1cyB7CiAgICAgICAgICAgIHB1YmxpYyBpbnQgZ2V0UHJpb3JpdHkoKSB7IHJldHVybiAxOyB9CiAgICAgICAgICAgIHB1YmxpYyBpbnQgb3BlcmF0ZShpbnQgbGhzLCBpbnQgcmhzKSB7IHJldHVybiBsaHMgKyByaHM7IH0KICAgICAgICAgICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsgcmV0dXJuICIrIjsgfQogICAgICAgIH0sCiAgICAgICAgTWludXMgewogICAgICAgICAgICBwdWJsaWMgaW50IGdldFByaW9yaXR5KCkgeyByZXR1cm4gMTsgfQogICAgICAgICAgICBwdWJsaWMgaW50IG9wZXJhdGUoaW50IGxocywgaW50IHJocykgeyByZXR1cm4gbGhzIC0gcmhzOyB9CiAgICAgICAgICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKSB7IHJldHVybiAiLSI7IH0KICAgICAgICB9LAogICAgICAgIFRpbWVzIHsKICAgICAgICAgICAgcHVibGljIGludCBnZXRQcmlvcml0eSgpIHsgcmV0dXJuIDI7IH0KICAgICAgICAgICAgcHVibGljIGludCBvcGVyYXRlKGludCBsaHMsIGludCByaHMpIHsgcmV0dXJuIGxocyAqIHJoczsgfQogICAgICAgICAgICBwdWJsaWMgU3RyaW5nIHRvU3RyaW5nKCkgeyByZXR1cm4gIioiOyB9CiAgICAgICAgfTsKCiAgICAgICAgcHVibGljIGFic3RyYWN0IGludCBnZXRQcmlvcml0eSgpOwogICAgICAgIHB1YmxpYyBhYnN0cmFjdCBpbnQgb3BlcmF0ZShpbnQgbGhzLCBpbnQgcmhzKTsKICAgIH0KICAgIHByaXZhdGUgc3RhdGljIGludGVyZmFjZSBUZXJtIHsKICAgICAgICBwdWJsaWMgYm9vbGVhbiBpc09wZXJhdGUoKTsKICAgICAgICBwdWJsaWMgaW50IGdldFZhbHVlKCk7CiAgICB9CiAgICBwcml2YXRlIHN0YXRpYyBjbGFzcyBUZXJtVmFsdWUgaW1wbGVtZW50cyBUZXJtIHsKICAgICAgICBwdWJsaWMgZmluYWwgaW50IHZhbHVlOwoKICAgICAgICBwdWJsaWMgVGVybVZhbHVlKGludCB2YWwpIHsKICAgICAgICAgICAgdmFsdWUgPSB2YWw7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyBib29sZWFuIGlzT3BlcmF0ZSgpIHsgcmV0dXJuIGZhbHNlOyB9CiAgICAgICAgcHVibGljIGludCBnZXRWYWx1ZSgpIHsKICAgICAgICAgICAgcmV0dXJuIHZhbHVlOwogICAgICAgIH0KICAgICAgICBwdWJsaWMgU3RyaW5nIHRvU3RyaW5nKCkgeyByZXR1cm4gU3RyaW5nLnZhbHVlT2YodmFsdWUpOyB9CiAgICB9CiAgICBwcml2YXRlIHN0YXRpYyBjbGFzcyBUZXJtRGF0YSBpbXBsZW1lbnRzIFRlcm0gewogICAgICAgIHB1YmxpYyBmaW5hbCBPcGVyYXRvciBvcDsKICAgICAgICBwdWJsaWMgZmluYWwgVGVybSBsaHM7CiAgICAgICAgcHVibGljIGZpbmFsIFRlcm0gcmhzOwoKICAgICAgICAvKioKICAgICAgICAgKiDlj7Povrrjga/ov73liqDliIbjgajogIPjgYjjgabjgIHmvJTnrpflrZDjga7poIbluo/jgavlvpPjgaPjgabjg4fjg7zjgr/jgpLmraPopo/ljJbjgZfjgb7jgZnjgIIKICAgICAgICAgKi8KICAgICAgICBwdWJsaWMgVGVybURhdGEoT3BlcmF0b3IgbywgVGVybSBsLCBUZXJtIHIpIHsKICAgICAgICAgICAgT3BlcmF0b3Igb3BlcmF0b3IgPSBvOwogICAgICAgICAgICBUZXJtIGx0ZXJtID0gbDsKICAgICAgICAgICAgVGVybSBydGVybSA9IHI7CgogICAgICAgICAgICBpZiAobC5pc09wZXJhdGUoKSkgewogICAgICAgICAgICAgICAgVGVybURhdGEgZGF0YSA9IChUZXJtRGF0YSkgbDsKICAgICAgICAgICAgICAgIGlmIChvcGVyYXRvci5nZXRQcmlvcml0eSgpID4gZGF0YS5vcC5nZXRQcmlvcml0eSgpKSB7CiAgICAgICAgICAgICAgICAgICAgcnRlcm0gPSBuZXcgVGVybURhdGEob3BlcmF0b3IsIGRhdGEucmhzLCBydGVybSk7CiAgICAgICAgICAgICAgICAgICAgbHRlcm0gPSBkYXRhLmxoczsKICAgICAgICAgICAgICAgICAgICBvcGVyYXRvciA9IGRhdGEub3A7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgb3AgPSBvcGVyYXRvcjsKICAgICAgICAgICAgbGhzID0gbHRlcm07CiAgICAgICAgICAgIHJocyA9IHJ0ZXJtOwogICAgICAgIH0KICAgICAgICBwdWJsaWMgYm9vbGVhbiBpc09wZXJhdGUoKSB7IHJldHVybiB0cnVlOyB9CiAgICAgICAgcHVibGljIGludCBnZXRWYWx1ZSgpIHsKICAgICAgICAgICAgcmV0dXJuIG9wLm9wZXJhdGUobGhzLmdldFZhbHVlKCksIHJocy5nZXRWYWx1ZSgpKTsKICAgICAgICB9CiAgICAgICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICAgICAgcmV0dXJuIGxocy50b1N0cmluZygpICsgb3AudG9TdHJpbmcoKSArIHJocy50b1N0cmluZygpOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgbG9uZyBzdGFydCA9IFN5c3RlbS5jdXJyZW50VGltZU1pbGxpcygpOwogICAgICAgIGtvbWFjaGkobmV3IGludFtdIHsxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5fSwgMTAwKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICBsb25nIGVuZCA9IFN5c3RlbS5jdXJyZW50VGltZU1pbGxpcygpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiZWxpcHNlOiAiICsgKGVuZCAtIHN0YXJ0KSArICIobXMpIik7CiAgICB9Cn0=