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

public class TestGen {

    static int done = 0;

    static final Random rnd = new Random(556668949238L);

    static int rand(int n) {
        if (n < 1)
            return 0;
        return rnd.nextInt(n);
    }

    static char randc() {
        return (char) ('a' + rand(26));
    }

    static final int MAXN1 = 1000;
    static final int MAXN2 = 100_000;
    static final int MAXX = 1_000_000_000;

    static PrintWriter desc;

    static void print(char[] s, int a, int b, int c) throws IOException {
        done++;
        System.err.println("Generation test #" + done);
        PrintWriter out;
        if (done < 10)
            out = new PrintWriter("../tests/0" + String.valueOf(done));
        else
            out = new PrintWriter("../tests/" + String.valueOf(done));
        out.println(s.length);
        for (int i = 0; i < s.length - 1; i++)
            out.print(s[i]);
        out.println(s[s.length - 1]);
        out.println(String.format("%d %d %d", a, b, c));
        out.close();
    }

    static String getPath() {
        if (done < 10)
            return "../tests/0" + String.valueOf(done);
        return "../tests/" + String.valueOf(done);
    }


    static void gen_samples() throws IOException {
        PrintWriter out;
        String path;
        path = "../preliminary/01";
        out = new PrintWriter(path);
        out.println("3");
        out.println("abc");
        out.println("1 1 1");
        out.close();
        print_desc(path, "preliminary", "subtask0", 0, "sample 1");
        path = "../preliminary/02";
        out = new PrintWriter(path);
        out.println("7");
        out.println("abcdabc");
        out.println("1 2 0");
        out.close();
        print_desc(path, "preliminary", "subtask0", 0, "sample 2");
    }

    static void print_desc(String path, String folder, String subtask, int points, String comment) throws IOException {
        desc.println(String.format("%s, folder=%s, subtask=%s, points=%d, comment=\"%s\"", path, folder, subtask, points, comment));
    }

    static String subtask;

    static void print_desc_t(String comment) throws IOException {
        print_desc(getPath(), "tests", subtask, 0, comment);
    }

    //begin gens


    static  void fill_rand(char[] s) {
        for (int i = 0; i < s.length; i++) {
            s[i] = randc();
        }
    }

    static void gen_rand(int maxn, int a, int b, int c) throws IOException {
        int n = rand(maxn / 2) + maxn / 2 + 1;
        char[] s = new char[n];
        fill_rand(s);
        print(s, a, b, c);
        print_desc_t(String.format("random test, a = %d, b = %d, c = %d", a, b, c));
    }

    static void gen_simples_subtask1() throws IOException {
        char[] s = "aaaaaaaaa".toCharArray();
        print(s, 1, 2, 0);
        print_desc_t(String.format("aaaaaaaaa, a = %d, b = %d, c = %d", 1, 2, 0));

        s = "bbbbbbb".toCharArray();
        print(s, 3, 1, 0);
        print_desc_t(String.format("bbbbbbb, a = %d, b = %d, c = %d", 3, 1, 0));

        s = "qwertyuiopasdfghjklzxcvbnm".toCharArray();
        print(s, 100, 0, 0);
        print_desc_t(String.format("qwertyuiopasdfghjklzxcvbnm, a = %d, b = %d, c = %d", 100, 0, 0));

        s = "abacabaabacabacabaabac".toCharArray();
        print(s, 100, 5, 0);
        print_desc_t(String.format("abacabaabacabacabaabac, a = %d, b = %d, c = %d", 100, 5, 0));
    }

    static void gen_simples_subtask2() throws IOException {
        char[] s = "aaaaaaaaa".toCharArray();
        print(s, 1, 2, 1);
        print_desc_t(String.format("aaaaaaaaa, a = %d, b = %d, c = %d", 1, 2, 1));

        s = "bbbbbbb".toCharArray();
        print(s, 3, 1, 1);
        print_desc_t(String.format("bbbbbbb, a = %d, b = %d, c = %d", 3, 1, 1));

        s = "qwertyuiopasdfghjklzxcvbnm".toCharArray();
        print(s, 100, 0, 30);
        print_desc_t(String.format("qwertyuiopasdfghjklzxcvbnm, a = %d, b = %d, c = %d", 100, 0, 30));

        s = "abacabaabacabacabaabac".toCharArray();
        print(s, 100, 5, 1);
        print_desc_t(String.format("abacabaabacabacabaabac, a = %d, b = %d, c = %d", 100, 5, 1));
    }

    static void gen_same(int n, char ch, int a, int b, int c) throws IOException {
        char s[] = new char[n];
        for (int i = 0; i < n; i++)
            s[i] = ch;
        print(s, a, b, c);
        print_desc_t(String.format("%d symbols %c, a = %d, b = %d, c = %d", n, ch, a, b, c));
    }

    static void gen_period(int maxn, String per, int a, int b, int c) throws IOException {
        int n = (maxn / per.length()) * per.length();
        char s[] = new char[n];
        for (int i = 0; i < n; i += per.length()) {
            for (int j = 0; j < per.length(); j++) {
                s[i + j] = per.charAt(j);
            }
        }
        print(s, a, b, c);
        print_desc_t(String.format("periodic %s, a = %d, b = %d, c = %d", per, a, b, c));
    }

    //end gens

    public static void main(String[] args) throws IOException {
        new File("../preliminary").mkdir();
        new File("../tests").mkdir();
        desc = new PrintWriter("../tests.desc");

        done = 0;
        gen_samples();

        subtask = "subtask1";

        gen_simples_subtask1();

        for (int i = 0; i < 3; i++) {
            int n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            char[] s = new char[rand(15) + 1];
            fill_rand(s);
            gen_period(n, new String(s), rand(MAXX), rand(MAXX / 2), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            s = new char[rand(20) + 1];
            fill_rand(s);
            gen_period(n, new String(s), rand(MAXX), rand(MAXX / 2), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            gen_rand(n, rand(MAXX), rand(20), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            gen_rand(n, rand(27), rand(MAXX), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            gen_rand(n, rand(MAXX), rand(MAXX), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            gen_same(n, randc(), rand(MAXX), rand(MAXX), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            gen_same(n, randc(), rand(50), rand(MAXX), 0);

            n = rand(MAXN1 / 2) + MAXN1 / 2 + 1;
            gen_same(n, randc(), rand(MAXX), rand(25), 0);

        }

        gen_period(MAXN1, "aba", rand(MAXX), rand(MAXX), 0);
        gen_period(MAXN1, "abc", MAXX, MAXX, 0);
        gen_rand(MAXN1, MAXX, MAXX, 0);
        gen_same(MAXN1, 'l', MAXX, MAXX, 0);

        desc.println(String.format("comment_subtask%d=\"n &lt;= %d, c = %d\"", 1, MAXN1, 0));
        desc.println(String.format("bonus_subtask%d=%d", 1, 50));

        subtask = "subtask2";

        gen_simples_subtask2();

        for (int i = 0; i < 4; i++) {
            int n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            char[] s = new char[rand(15) + 1];
            fill_rand(s);
            gen_period(n, new String(s), rand(MAXX), rand(MAXX / 2), rand(MAXX / 2));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            s = new char[rand(20) + 1];
            fill_rand(s);
            gen_period(n, new String(s), rand(MAXX), rand(MAXX / 2), rand(MAXX / 2));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            gen_rand(n, rand(MAXX), rand(20), rand(MAXX));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            gen_rand(n, rand(27), rand(MAXX), rand(MAXX / 2));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            gen_rand(n, rand(MAXX), rand(MAXX), rand(MAXX / 2));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            gen_same(n, randc(), rand(MAXX), rand(MAXX), rand(10));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            gen_same(n, randc(), rand(50), rand(MAXX), rand(20));

            n = rand(MAXN2 / 2) + MAXN2 / 2 + 1;
            gen_same(n, randc(), rand(MAXX), rand(25), rand(MAXX / 2));

        }

        gen_period(MAXN2, "aba", rand(MAXX), rand(MAXX), rand(MAXX));
        gen_period(MAXN2, "abc", MAXX, MAXX, MAXX);
        gen_rand(MAXN2, MAXX, MAXX, MAXX);
        gen_same(MAXN2, 'z', MAXX, MAXX, MAXX);


        desc.println(String.format("comment_subtask%d=\"n &lt;= %d\"", 2, MAXN2));
        desc.println(String.format("bonus_subtask%d=%d", 2, 50));


        desc.close();
    }
}
