Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.Calendar;
- public class Main {
- private static char[] word;
- public static void main(String[] args) {
- load("slowo2.txt");
- long start_time = Calendar.getInstance().getTimeInMillis();
- int found = 0;
- for (int i = 1; i < word.length / 2 + 1; i++) {
- if (Arrays.equals(word, 0, i - 1, word, i, i + i - 1)) found = i;
- //if (isEqual(i)) found = i;
- }
- long stop_time = Calendar.getInstance().getTimeInMillis();
- System.out.println("Found: " + (found * 2) + " in " + (stop_time - start_time)/1000 + " s.");
- }
- // public static boolean isEqual(int to) {
- //
- // for (int i = 0; i < to; i++) {
- // if (word[i] != word[i + to]) {
- // return false;
- // }
- // }
- //
- // return true;
- // }
- private static void load(String path) {
- File file = new File(path);
- word = new char[(int) file.length()];
- try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
- reader.read(word);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement