Submission #3392738


Source Code Expand

Copy
import java.io.*;
import java.util.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    long gcd(long x, long y) {
        return x == 0 ? y : gcd(y % x, x);
    }

    long lcm(long x, long y) {
        return x / gcd(x, y) * y;
    }

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        char[] a = in.next().toCharArray();
        char[] b = in.next().toCharArray();
        long len = lcm(n, m);
        for (int i = 0; i < n; i++) {
            long pos = i * (len / n);
            if (pos % (len / m) == 0) {
                long j = pos / (len / m);
                if (a[i] != b[(int) j]) {
                    out.println(-1);
                    return;
                }
            }
        }
        out.println(len);
    }

    void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    void runIO() {

        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

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

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

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

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

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

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

    public static void main(String[] args) {
        new Main().runIO();
    }
}

Submission Info

Submission Time
Task A - Two Abbreviations
User qwerty787788
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 2992 Byte
Status
Exec Time 122 ms
Memory 25812 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample-01.txt, sample-02.txt, sample-03.txt
All 300 / 300 sample-01.txt, sample-02.txt, sample-03.txt, sample-01.txt, sample-02.txt, sample-03.txt, subtask01-01.txt, subtask01-02.txt, subtask01-03.txt, subtask01-04.txt, subtask01-05.txt, subtask01-06.txt, subtask01-07.txt, subtask01-08.txt, subtask01-09.txt, subtask01-10.txt, subtask01-11.txt, subtask01-12.txt, subtask01-13.txt, subtask01-14.txt, subtask01-15.txt
Case Name Status Exec Time Memory
sample-01.txt 70 ms 21204 KB
sample-02.txt 70 ms 20180 KB
sample-03.txt 69 ms 19796 KB
subtask01-01.txt 70 ms 22484 KB
subtask01-02.txt 85 ms 20948 KB
subtask01-03.txt 88 ms 22228 KB
subtask01-04.txt 86 ms 23380 KB
subtask01-05.txt 96 ms 22484 KB
subtask01-06.txt 87 ms 25812 KB
subtask01-07.txt 73 ms 19924 KB
subtask01-08.txt 93 ms 22996 KB
subtask01-09.txt 84 ms 21460 KB
subtask01-10.txt 122 ms 24276 KB
subtask01-11.txt 93 ms 24660 KB
subtask01-12.txt 91 ms 21332 KB
subtask01-13.txt 96 ms 22996 KB
subtask01-14.txt 90 ms 23124 KB
subtask01-15.txt 88 ms 22484 KB