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

public class TestGen {

	static int done = 0;

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

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

	static final int MAXN = 1000;
	static final int MAXK = 100;

	static ArrayList<String> data = new ArrayList<String>();

	static void print() 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));
		for (int i = 0; i < data.size(); i++)
			out.println(data.get(i));
		data.clear();
		out.close();
	}

	static void gen_sample() throws IOException {
		PrintWriter out;
		out = new PrintWriter("../preliminary/01");
		out.println("2 1");
		out.println("Russia Russia go go go");
		out.println("Russia Russia lets go");
		out.close();
		out = new PrintWriter("../preliminary/02");
		out.println("3 2");
		out.println("Russia is champion");
		out.println("Russia is winner");
		out.println("go go go");
		out.close();
	}

	static char chars[] = new char[53];

	static void fill() {
		for (char c = 'a'; c <= 'z'; c++)
			chars[c - 'a'] = c;
		for (char c = 'A'; c <= 'Z'; c++)
			chars[c - 'A' + 'z' - 'a' + 1] = c;
		chars[52] = ' ';
	}

	static char randc() {
		return chars[rand(53)];
	}

	static String rands(int n) {
		String ans = "";
		char c;
		ans += chars[rand(52)];
		for (int i = 1; i < n - 1; i++) {
			c = randc();
			ans = ans + c;
		}
		if(n > 1)
			ans = ans + chars[rand(52)];
		return ans;
	}

	static void gen_rand_case(int n, int m, int k, int len) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		for (int i = 0; i < n; i++) {
			data.add(rands(rand(len - k) + k));
		}
		print();
	}

	static void gen_right_case(int n, int m, int k, int len) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		int a = data.size();
		for (int i = 0; i < n; i++) {
			if (i < m)
				data.add(rands(rand(len - k) + k));
			else {
				// System.out.println(i + " " + m + " " + a);
				String q = data.get(i - m + a);
				String s = rands(rand(len - k))
						+ q.substring(q.length() - k, q.length());
				data.add(s);
			}
		}
		print();
	}

	static void gen_left_case(int n, int m, int k, int len) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		int errorstring = rand(n - m) + m;
		int a = data.size();
		for (int i = 0; i < n; i++) {
			if (i < m)
				data.add(rands(rand(len - k) + k));
			else {
				String q = data.get(i - m + a);
				String s;
				if (i != errorstring)
					s = rands(rand(len - k))
							+ q.substring(q.length() - k, q.length());
				else {
					int ind = rand(k);
					s = rands(rand(len - k))
							+ q.substring(q.length() - k, q.length() - k + ind)
							+ randc()
							+ q.substring(q.length() - k + ind + 1, q.length());
				}
				data.add(s);
			}
		}
		print();
	}

	static void gen_single_killer(int n, int m, int k, int len) throws IOException {
		data.add(String.valueOf(n) + " " + String.valueOf(m));
		int a = data.size();
		for (int i = 0; i < n; i++) {
			if (i < m)
				data.add(rands(rand(len - k) + k));
			else {
				String q = data.get(i - m + a);
				String s;
				s = rands(rand(len - k + 1))
						+ q.substring(q.length() - k + 1, q.length());
				data.add(s);
			}
		}
		print();
	}

	

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

		gen_sample();

		done = 0;
		fill();
		for (int i = 0; i < 53; i++)
			System.err.print(chars[i]);
		System.err.println();
		gen_rand_case(12, 4, 1, 20);
		gen_right_case(10, 3, 4, 20);
		gen_left_case(17, 13, 12, 20);
		gen_single_killer(20, 1, 14, 20);
		gen_right_case(rand(25) + 30, rand(25) + 3, rand(50) + 1, 100);
		gen_right_case(rand(500) + 500, rand(17) + 37, rand(54) + 10, 100);
		gen_rand_case(1000, 4, 99, 100);
		gen_right_case(1000, 56, 99, 100);
		gen_left_case(1000, 123, 99, 100);
		gen_single_killer(1000, 321, 99, 100);
		
		gen_rand_case(12, 1, 10, 200);
		gen_right_case(10, 5, 40, 200);
		gen_left_case(17, 14, 120, 200);
		gen_single_killer(20, 9, 140, 200);
		gen_right_case(rand(25) + 30, rand(25) + 3, rand(50) + 1, 1000);
		gen_right_case(rand(500) + 500, rand(17) + 37, rand(54) + 10, 1000);
		gen_rand_case(1000, 777, 999, 1000);
		gen_right_case(1000, 666, 999, 1000);
		gen_left_case(1000, 356, 999, 1000);
		gen_single_killer(1000, 999, 999, 1000);
	
		return;
	}
}
