Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class SubString
- {
- final static int bufferSize = 1024 * 64;
- static byte[] buffer = new byte[bufferSize];
- public static String readLine(InputStream stream) throws IOException
- {
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- stream.mark(bufferSize);
- int readBytes = stream.read(buffer, 0, bufferSize);
- for (int i = 0; i < readBytes - 1; ++i)
- {
- if (buffer[i] == '\n')
- {
- stream.reset();
- readBytes = stream.read(buffer, 0, i + 1) - 1;
- break;
- }
- }
- if (readBytes > 0 && buffer[readBytes - 1] == '\n')
- readBytes--;
- for (int i = 0; i < readBytes; ++i)
- sb.append((char) buffer[i]);
- if (readBytes < bufferSize)
- break;
- }
- return sb.toString();
- }
- public static int getNum(String t)
- {
- int num = 0;
- int k = t.length();
- int reminder;
- int baghimande = 0;
- for (int j = 0; j < k; j++)
- {
- int n = t.charAt(j) - 'a' + 1;
- if (baghimande != 0)
- baghimande = (27 * baghimande) % 200009;
- else
- baghimande = 1;
- num = (num + n * baghimande) % 200009;
- }
- return num;
- }
- public static int getNum2(String t)
- {
- int num = 0;
- int k = t.length();
- int reminder;
- int baghimande = 0;
- for (int j = 0; j < k; j++)
- {
- int n = t.charAt(j) - 'a' + 1;
- if (baghimande != 0)
- baghimande = (27 * baghimande) % 100003;
- else
- baghimande = 1;
- num = (num + n * baghimande) % 100003;
- }
- return num;
- }
- public static void main(String[] args)
- {
- BufferedInputStream stream = new BufferedInputStream(System.in);
- try
- {
- String s = readLine(stream);
- String line2 = readLine(stream);
- int k = Integer.parseInt(line2);
- int l = s.length();
- int h = l - k + 1;
- boolean flag = false;
- int a = -1;
- int[] heapTable = new int[300012];
- int temp;
- int min = -1;
- for (int i = 0; i < h; i++)
- {
- String t = s.substring(i, i + k);
- temp = getNum(t) + getNum2(t);
- if (heapTable[temp] != 0)
- {
- flag = true;
- if (min == -1 || min > (heapTable[temp] - 1))
- min = heapTable[temp] - 1;
- } else
- heapTable[temp] = i + 1;
- }
- if (flag)
- System.out.println(min);
- else
- System.out.println("No Solution");
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement