제출 #112988

#제출 시각아이디문제언어결과실행 시간메모리
112988model_codeLock Puzzle (innopolis2018_final_A)Java
100 / 100
206 ms11840 KiB
import java.io.*;
import java.util.*;

public class A {

    void solve() {
        int n = in.nextInt(), m = in.nextInt();
        String s = in.next(), t = in.next();
        List<Pair> pairsS = new ArrayList<Pair>(), pairsT = new ArrayList<Pair>();
        for (int i = 0; i < n; i++) {
            pairsS.add(new Pair(s.charAt(i), i));
            pairsT.add(new Pair(t.charAt(i), i));
        }
        Collections.sort(pairsS);
        Collections.sort(pairsT);
        int[] permutation = new int[n];
        for (int i = 0; i < n; i++) {
            if (pairsS.get(i).ch != pairsT.get(i).ch) {
                out.println(-1);
                return;
            }
            permutation[pairsS.get(i).pos] = pairsT.get(i).pos;
        }

        solveForPermutation(permutation);
        out.println(moves.size());
        for (int i : moves) {
            out.print((n - i) + " ");
        }
    }

    List<Integer> moves = new ArrayList<>();

    private void solveForPermutation(int[] permutation) {
        int n = permutation.length;
        int left = permutation[n - 1], right = left;
        int size = 1;
        boolean increasing = true;
        while (size < n - 1) {
            int nextVal = increasing ? (left + n - 1) % n : (left + 1) % n;
            makeAMove(permutation, pos(permutation, nextVal) + 1);
            makeAMove(permutation, 0);
            size++;
            makeAMove(permutation, pos(permutation, left));
            if (size == n - 1) {
                break;
            }

            int newLeft = right;
            int newRight = nextVal;
            left = newLeft;
            right = newRight;
            increasing = !increasing;

            nextVal = increasing ? (left + n - 1) % n : (left + 1) % n;
            makeAMove(permutation, pos(permutation, nextVal));
            makeAMove(permutation, pos(permutation, right) + 1);
            size++;

            left = nextVal;
        }

        finishWithCyclicShift(permutation);
        if (moves.size() > 2.5 * permutation.length) {
            throw new AssertionError();
        }
    }

    private void finishWithCyclicShift(int[] permutation) {
        int n = permutation.length;
        for (int d : new int[]{-1, 1}) {
            boolean ok = true;
            for (int i = 0; i < permutation.length; i++) {
                ok &= (permutation[(i + 1) % n] - permutation[i] + n - d) % n == 0;
            }
            if (ok) {
                if (d == -1) {
                    int size = permutation[0];
                    makeAMove(permutation, size + 1);
                    makeAMove(permutation, n - size - 1);
                } else {
                    int size = n - permutation[0];
                    makeAMove(permutation, size);
                    makeAMove(permutation, n - size);
                    makeAMove(permutation, 0);
                }
                return;
            }
        }
        throw new AssertionError();
    }

    int pos(int[] a, int val) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == val) {
                return i;
            }
        }
        return -1;
    }

    void makeAMove(int[] a, int pos) {
        moves.add(pos);
        reverse(a, 0, pos - 1);
        reverse(a, 0, a.length - 1);
    }

    void reverse(int[] a, int from, int to) {
        for (int i = from, j = to; i < j; i++, j--) {
            int tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
    }

    class Pair implements Comparable<Pair> {
        char ch;
        int pos;

        public Pair(char ch, int pos) {
            this.ch = ch;
            this.pos = pos;
        }


        @Override
        public int compareTo(Pair o) {
            if (ch != o.ch) {
                return Character.compare(ch, o.ch);
            }
            return Integer.compare(pos, o.pos);
        }
    }

    FastScanner in;
    PrintWriter out;

    void run() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }

    public class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public boolean hasMoreTokens() {
            while (st == null || !st.hasMoreElements()) {
                String line = null;
                try {
                    line = br.readLine();
                } catch (IOException e) {
                    return false;
                }
                if (line == null) {
                    return false;
                }
                st = new StringTokenizer(line);
            }
            return true;
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                String line = null;
                try {
                    line = br.readLine();
                } catch (IOException e) {
                }
                st = new StringTokenizer(line);
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] ret = new int[n];
            for (int i = 0; i < n; i++) {
                ret[i] = nextInt();
            }
            return ret;
        }
    }


    public static void main(String[] args) {
        new A().run();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...