LoginSignup
0
0

More than 5 years have passed since last update.

第7回オフラインリアルタイムどう書く(Java解答)

Posted at
SelectChair.java
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class SelectChair {

    protected final char EMPTY = '-';

    public String solve(final String string) {
        String[] split = string.split(":");
        int chairNum = Integer.valueOf(split[0]);
        char[] chairs = initChairs(chairNum);

        char[] inOut = getInOut(split[1]);
        for (char each : inOut) {
            if (Character.isUpperCase(each)) {
                int recomended = searchChair(chairs);
                chairs[recomended] = each;
            } else {
                int awayIndex = awayChair(chairs, each);
                chairs[awayIndex] = EMPTY;
            }
        }
        String result = "";
        for (char each : chairs) {
            result += each;
        }
        return result;
    }

    protected char[] initChairs(final int chairNum) {
        char[] result = new char[chairNum];
        for (int i = 0; i < result.length; i++) {
            result[i] = EMPTY;
        }
        return result;
    }

    protected int searchChair(final char[] chairs) {
        List<Integer> primary = new ArrayList<>();
        List<Integer> secondary = new ArrayList<>();
        List<Integer> guttari = new ArrayList<>();

        for (int i = 0; i < chairs.length; i++) {
            char tmpChair = chairs[i];
            if (tmpChair == EMPTY) {
                if (chairs.length == 1) {
                    primary.add(i);
                } else if ((i == 0 && EMPTY == chairs[i + 1])
                        || (i == chairs.length - 1 && EMPTY == chairs[i - 1])) {
                    primary.add(i);
                } else if ((i == 0 && EMPTY != chairs[i + 1])
                        || (i == chairs.length - 1 && EMPTY != chairs[i - 1])) {
                    secondary.add(i);
                } else {
                    if (EMPTY == chairs[i + 1] && EMPTY == chairs[i - 1]) {
                        primary.add(i);
                    } else if (EMPTY == chairs[i + 1] || EMPTY == chairs[i - 1]) {
                        secondary.add(i);
                    }
                }
                guttari.add(i);
            }
        }

        int result = -1;
        if (0 < primary.size()) {
            result = getChair(primary);
        } else if (0 < secondary.size()) {
            result = getChair(secondary);
        } else if (0 < guttari.size()) {
            result = getChair(guttari);
        }
        return result;
    }

    protected int awayChair(final char[] chairs, final char c) {
        for (int i = 0; i < chairs.length; i++) {
            if (chairs[i] == Character.toUpperCase(c)) {
                return i;
            }
        }
        return -1;
    }

    protected int getChair(final List<Integer> chairs) {
        int min = chairs.get(0);
        for (Integer each : chairs) {
            if (each < min) {
                min = each;
            }
        }
        return min;
    }

    protected char[] getInOut(final String string) {
        char[] result = new char[string.length()];
        for (int i = 0; i < string.length(); i++) {
            result[i] = string.charAt(i);
        }
        return result;
    }

    @Test
    public void testSelectChairs() throws Exception {
        /* 1 */test("6:NABEbBZn", "-ZAB-E");
        /* 2 */test("1:A", "A");
        /* 3 */test("1:Aa", "-");
        /* 4 */test("2:AB", "AB");
        /* 5 */test("2:AaB", "B-");
        /* 6 */test("2:AZa", "-Z");
        /* 7 */test("2:AZz", "A-");
        /* 8 */test("3:ABC", "ACB");
        /* 9 */test("3:ABCa", "-CB");
        /* 10 */test("4:ABCD", "ADBC");
        /* 11 */test("4:ABCbBD", "ABDC");
        /* 12 */test("4:ABCDabcA", "-D-A");
        /* 13 */test("5:NEXUS", "NUESX");
        /* 14 */test("5:ZYQMyqY", "ZM-Y-");
        /* 15 */test("5:ABCDbdXYc", "AYX--");
        /* 16 */test("6:FUTSAL", "FAULTS");
        /* 17 */test("6:ABCDEbcBC", "AECB-D");
        /* 18 */test("7:FMTOWNS", "FWMNTSO");
        /* 19 */test("7:ABCDEFGabcdfXYZ", "YE-X-GZ");
        /* 20 */test("10:ABCDEFGHIJ", "AGBHCIDJEF");
    }

    protected void test(String in, String out) {
        SelectChair sc = new SelectChair();
        assertEquals(out, sc.solve(in));
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0