Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4.  
  5. public class SubString
  6. {
  7.  
  8.     final static int bufferSize = 1024 * 64;
  9.     static byte[] buffer = new byte[bufferSize];
  10.  
  11.     public static String readLine(InputStream stream) throws IOException
  12.     {
  13.         StringBuilder sb = new StringBuilder();
  14.         while (true)
  15.         {
  16.             stream.mark(bufferSize);
  17.             int readBytes = stream.read(buffer, 0, bufferSize);
  18.             for (int i = 0; i < readBytes - 1; ++i)
  19.             {
  20.                 if (buffer[i] == '\n')
  21.                 {
  22.                     stream.reset();
  23.                     readBytes = stream.read(buffer, 0, i + 1) - 1;
  24.                     break;
  25.                 }
  26.             }
  27.             if (readBytes > 0 && buffer[readBytes - 1] == '\n')
  28.                 readBytes--;
  29.             for (int i = 0; i < readBytes; ++i)
  30.                 sb.append((char) buffer[i]);
  31.             if (readBytes < bufferSize)
  32.                 break;
  33.         }
  34.         return sb.toString();
  35.     }
  36.  
  37.     public static int getNum(String t)
  38.     {
  39.         int num = 0;
  40.         int k = t.length();
  41.         int reminder;
  42.         int baghimande = 0;
  43.         for (int j = 0; j < k; j++)
  44.         {
  45.             int n = t.charAt(j) - 'a' + 1;
  46.             if (baghimande != 0)
  47.                 baghimande = (27 * baghimande) % 200009;
  48.             else
  49.                 baghimande = 1;
  50.             num = (num + n * baghimande) % 200009;
  51.         }
  52.         return num;
  53.     }
  54.  
  55.     public static int getNum2(String t)
  56.     {
  57.         int num = 0;
  58.         int k = t.length();
  59.         int reminder;
  60.         int baghimande = 0;
  61.         for (int j = 0; j < k; j++)
  62.         {
  63.             int n = t.charAt(j) - 'a' + 1;
  64.             if (baghimande != 0)
  65.                 baghimande = (27 * baghimande) % 100003;
  66.             else
  67.                 baghimande = 1;
  68.             num = (num + n * baghimande) % 100003;
  69.         }
  70.         return num;
  71.     }
  72.  
  73.     public static void main(String[] args)
  74.     {
  75.         BufferedInputStream stream = new BufferedInputStream(System.in);
  76.         try
  77.         {
  78.             String s = readLine(stream);
  79.             String line2 = readLine(stream);
  80.             int k = Integer.parseInt(line2);
  81.             int l = s.length();
  82.             int h = l - k + 1;
  83.             boolean flag = false;
  84.             int a = -1;
  85.             int[] heapTable = new int[300012];
  86.             int temp;
  87.             int min = -1;
  88.             for (int i = 0; i < h; i++)
  89.             {
  90.                 String t = s.substring(i, i + k);
  91.                 temp = getNum(t) + getNum2(t);
  92.                 if (heapTable[temp] != 0)
  93.                 {
  94.                     flag = true;
  95.                     if (min == -1 || min > (heapTable[temp] - 1))
  96.                         min = heapTable[temp] - 1;
  97.                 } else
  98.                     heapTable[temp] = i + 1;
  99.             }
  100.             if (flag)
  101.                 System.out.println(min);
  102.             else
  103.                 System.out.println("No Solution");
  104.         } catch (IOException e)
  105.         {
  106.             e.printStackTrace();
  107.         }
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement