import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class gench1
{ 
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   gench1 bnk = new gench1();
   bnk.start();
}
//_____________________________________________________________
gench1() throws Exception 
{
}
//___________________________________________________________________________
void start()
{
   System.out.println("This proves pattern 60.");
   String str1 = "BRAJYO BRA ALHYM AO HJMYM VAO HARE";   
   startit(str1,false);
   startit(str1,true);
   String str2 = "BRAJYOB BRAA ALHYMA AOH HJMYMV VAOH HARE";   
   startit(str2,false);
   startit(str2,true);

}
//___________________________________________________________________________
void startit(String strr, boolean reversed)
{
   if (reversed) System.out.println("\r\n\r\nREVERSED\r\n");
   StringBuffer buf3 = new StringBuffer();
   String[] file = getFile("gench1.txt").split("\r\n");
   String[] words = strr.split(" ");
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < file.length; i++)
   {
      String[] words2 = file[i].split(" ");
      for (int j = 0; j < words2.length; j++)
      {
         buf.append(words2[j]);
      }
   }
   StringBuffer buf2 = new StringBuffer();
   byte[] letters = buf.toString().getBytes();
   byte[] letters2 = reverse(letters);
   if (reversed) letters = letters2;
   String[] output = new String[letters.length];
   for (int i = 0; i < output.length; i++)
   {
      output[i] = "";
   }
   System.out.println("letters = " + letters.length);
   for (int i = 0; i < words.length; i++)
   {
      byte[] bc = words[i].getBytes();
      boolean found = true;
      int k = 0;
      int j = 0;
      for (j = 0; j < letters.length; j++)
      {
         for (k = 2; k < letters.length; k++)
         {
            found = true;
            buf2 = new StringBuffer();
            for (int m = 0; m < bc.length; m++)
            {
               int pos = (j + (m * k)) % letters.length;
               if (letters[pos] != bc[m])
               {
                  found = false;
                  break;
               }
               else
               {
                  buf2.append(pos + " ");
               }
            }
            if (found) 
            {
               String[] poss = buf2.toString().split(" ");
               for (int n = 0; n < poss.length; n++)
               {
                  int ps = Integer.parseInt(poss[n]);
                  output[ps] += words[i] + " ";
               }
               break;
            }
         }
         if (found) break;
      }
      if (found)
      {
         buf3.append(words[i] + " " + i + " " + j + " " + k + " | " + buf2.toString() + "\r\n");

double prob = Math.pow(22,words[i].length()) / letters.length / k; 
System.out.println("(22 ^ " + words[i].length() + ") / (" + letters.length + " x " + k + ") = " + prob);

      }
      else
      {
         buf3.append(words[i] + " " + i + " not found\r\n");
      }
   }
   for (int i = 0; i < output.length; i++)
   {
      if (!output[i].equals(""))
      {
         buf3.append(i + " " + (char)letters[i] + " " + output[i] + "\r\n");
      }
   }
   System.out.println(buf3.toString());
   saveFile("gench1_output.txt",buf3.toString(),false);
}
//___________________________________________________________________________
public static String getFile(String file) 
{
   StringBuffer buf=new StringBuffer();String str;
  try
  {
   BufferedReader in = new BufferedReader (new FileReader (file));
   while((str=in.readLine())!=null)
   {
      buf.append(str+"\r\n");
   }
   in.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return (buf.toString());
}
//___________________________________________________________________________
public static void saveFile(String file, String str, boolean append) 
{
  try
  {
   BufferedWriter out = new BufferedWriter(new FileWriter(file, append));
   out.write(str);
   out.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
}
//___________________________________________________________________________
byte[] reverse(byte[] in)
{
   int pos = 0;
   byte[] out = new byte[in.length];
   for (int i = in.length-1; i >= 0; i--)
   {
      out[pos++] = in[i];  
   }
   return(out);
}
}//__________________________________________________________________________
