import java.io.*;
import java.util.*;
//_____________________________________________________________
public class shift
{ 
   byte[] gen11;
   int tot = 0;
   int tot37 = 0;
   int addit = 0;
   int grand = 0;
   int[] myval = new int[28];
   int[] mypla = new int[28];
   Random rand = new Random(0);
   String gen11t  = "BRAJYO BRA ALHYM AO HJMYM VAO HARE";
   int ddt = 0;
   int ddtcnt = 0;
   String out = "";
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   shift bnk = new shift();
   bnk.start(args);
}
//_____________________________________________________________
shift() throws Exception 
{
}
//___________________________________________________________________________
int start(String[] args)
{
   System.out.println("This proves pattern 50.");
   if (args.length > 0)
   {
      gen11t = "";
      for (int i = 0; i < args.length; i++)
      {
         if (i > 0) gen11t += " ";
         gen11t += args[i];
      }
   }
   startit(-1,gen11t.replace(" ",""));
   for (int i = 0; i < 1000; i++)
   {
      tot = 0;
      tot37 = 0;
      int ttt = startit(i,mix(i,gen11t.replace(" ","")));
      if (ttt >= 0) return(ttt);
   }
   return(0);
}
//___________________________________________________________________________
int startit(int ii, String gen)
{
   int val = ii;
   HashMap hash = new HashMap();
   int hashpos = 0;
   StringBuffer buf = new StringBuffer();
   String[] file = getFile("torah.txt").split("\r\n");
   String gen11v = gen; 
   gen11 = gen11v.getBytes();
   for (int i = 0; i < gen11v.length(); i++)
   {
      String s = gen11v.substring(i,i+1);
      mypla[i] = place(s);
      myval[i] = value(s);
   }
   for (int i = 0; i < file.length; i++)
   {
      String[] words = file[i].split(" ");
      for (int j = 0; j < words.length; j++)
      {
         if (hash.get(words[j]) == null)
         {
            hash.put(words[j],hashpos++);
            find(words[j]);
         }
      }
   }
   boolean show = false;
   if (tot37 > 40) show = true;
   StringBuffer buftt = new StringBuffer();
   int dd = (int) (1000 * (double)tot37 / (double)tot);
   String ddstr = Integer.toString(dd);
   ddstr = "00000".substring(ddstr.length()) + ddstr;
   if (ii < 0) ddt = dd;
   if (dd >= ddt) 
   {
      if (tot37 >= 11)
      {
         ddtcnt++;
         buftt.append(ddstr + " " + val + " " + tot + " " + tot37 + "\r\n");
         System.out.print(ddtcnt + " " + buftt.toString());
         return(val);
      }
   }
   return(-1);
}
//___________________________________________________________________________
boolean find(String str)
{
   StringBuffer buf = new StringBuffer();
   buf.append(str + " ");
   byte[] bc = str.getBytes();
   int pos = 0;
   int total = 0;
   for (int i = 0; i < bc.length; i++)
   {
      boolean found = false;
      for (int j = pos; j < gen11.length; j++)
      {
         if (gen11[j] == bc[i])
         {
            buf.append((j+0) + " ");
            total += myval[j+0] / mypla[j];
            pos = j+1;
            found = true;
            break;
         }
      }
      if (!found) return(false);
   }
   total += addit;
   grand += total;
   tot++;
   if (total % 37 == 0) tot37++;
   return(true);
}
//___________________________________________________________________________
String mix(int ii, String str)
{
   rand = new Random(ii);
   byte[] bb = str.getBytes();
   for (int i = 0; i < 100; i++)
   {
      int rnd1 = rand.nextInt(bb.length);
      int rnd2 = rand.nextInt(bb.length);
      byte bc = bb[rnd1];
      bb[rnd1] = bb[rnd2];
      bb[rnd2] = bc; 
   }
   String out = new String(bb);
   return(out);
}
//___________________________________________________________________________
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());
}
//__________________________________________________
int value(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 20;
      else if (st == 'l') v = 30;    
      else if (st == 'm') v = 40;
      else if (st == 'n') v = 50;
      else if (st == 'o') v = 60;
      else if (st == 'p') v = 70;
      else if (st == 'q') v = 80;
      else if (st == 'r') v = 90;
      else if (st == 's') v = 100;
      else if (st == 't') v = 200;
      else if (st == 'u') v = 300;
      else if (st == 'v') v = 400;
      else if (st == 'w') v = 500;
      else if (st == 'x') v = 600;
      else if (st == 'y') v = 700;
      else if (st == 'z') v = 800;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 20;
      else if (st == 'L') v = 30;
      else if (st == 'M') v = 40;
      else if (st == 'N') v = 50;
      else if (st == 'S') v = 60;
      else if (st == 'I') v = 70;
      else if (st == 'F') v = 80;
      else if (st == 'E') v = 90;
      else if (st == 'Q') v = 100;
      else if (st == 'R') v = 200;
      else if (st == 'J') v = 300;
      else if (st == 'O') v = 400;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
//__________________________________________________
int place(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 11;
      else if (st == 'l') v = 12;    
      else if (st == 'm') v = 13;
      else if (st == 'n') v = 14;
      else if (st == 'o') v = 15;
      else if (st == 'p') v = 16;
      else if (st == 'q') v = 17;
      else if (st == 'r') v = 18;
      else if (st == 's') v = 19;
      else if (st == 't') v = 20;
      else if (st == 'u') v = 21;
      else if (st == 'v') v = 22;
      else if (st == 'w') v = 23;
      else if (st == 'x') v = 24;
      else if (st == 'y') v = 25;
      else if (st == 'z') v = 26;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 11;
      else if (st == 'L') v = 12;
      else if (st == 'M') v = 13;
      else if (st == 'N') v = 14;
      else if (st == 'S') v = 15;
      else if (st == 'I') v = 16;
      else if (st == 'F') v = 17;
      else if (st == 'E') v = 18;
      else if (st == 'Q') v = 19;
      else if (st == 'R') v = 20;
      else if (st == 'J') v = 21;
      else if (st == 'O') v = 22;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
}//__________________________________________________________________________
