NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package semestralka;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author STUDENT: Kopecká, Nikol
*/
public class MaxPalindroms {

static String path = "DNA.txt";
static ArrayList<Palindrom> sude = new ArrayList<>();
static ArrayList<Palindrom> liche = new ArrayList<>();
static int maxLengthSude;
static int maxLengthLiche;
static String DNA = null;

public static void main(String[] args) throws Exception {
long tstart = System.currentTimeMillis();
File fileRead = new File(path);
FileInputStream fis = null;

try {
fis = new FileInputStream(fileRead);

System.out.println("Total file size to read (in bytes) : "
+ fis.available());

BufferedReader br = new BufferedReader(new FileReader(fileRead));
String line = null;
StringBuilder stringBuilder = new StringBuilder();

while ((line = br.readLine()) != null) {
stringBuilder.append(line);
}
DNA = stringBuilder.toString();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("");
System.out.println("START");
long t0 = System.currentTimeMillis();

int findMaxLength = findLongestPalindrome(DNA).length();
findAllPalindromesLelngth(findMaxLength);
if (findMaxLength % 2 == 0) {
int l = findMaxLength - 1;
while (maxLengthLiche == 0) {
findAllPalindromesLelngth(l);
l = l - 2;
}
}

System.out.println("");
if (!sude.isEmpty()) {
System.out.println("EVEN_PALINDROM MAX_LENTGTH = " + maxLengthSude);
sude.stream().forEach((p) -> {
System.out.println(p.getStarPosition() + "t" + p.getPalindrom());
});
System.out.println("");
}
if (!liche.isEmpty()) {
System.out.println("EVEN_PALINDROM MAX_LENTGTH = " + maxLengthLiche);
liche.stream().forEach((p) -> {
System.out.println(p.getStarPosition() + "t" + p.getPalindrom());
});
}
long t1 = System.currentTimeMillis();
System.out.println("END TIME= " + (t1 - t0) + " SEC");
System.out.println("END TIME= " + (t1 - tstart) + " SEC");
}

public static void findAllPalindromesLelngth(int length) {
if (length % 2 == 0) {
for (int i = 0; i < DNA.length() - length; i++) {
if (isPalindromeB(i, DNA.substring(i, i + length))) {
sude.add(new Palindrom(i, DNA.substring(i, i + length)));
maxLengthSude = length;
}

}
}
if (length % 2 != 0) {
for (int i = 0; i < DNA.length() - length; i++) {
if (isPalindromeB(i, DNA.substring(i, i + length))) {
liche.add(new Palindrom(i, DNA.substring(i, i + length)));
maxLengthLiche = length;
}

}
}
}

private static boolean isPalindromeB(int position, String substring) {
if (substring.length() == 0) {
return true;
}
if (substring.length() == 1) {
return true;
}

if (substring.charAt(0) == (substring.charAt(substring.length() - 1))) {
return isPalindromeB(position, substring.substring(1, substring.length() - 1));
}
return false;
}

/**
* ManachersAlgorithm
*
* @param s
* @return
*/
public static String findLongestPalindrome(String s) {
if (s == null || s.length() == 0) {
return "";
}

char[] s2 = addBoundaries(s.toCharArray());
int[] p = new int[s2.length];
int c = 0, r = 0; // Here the first element in s2 has been processed.
int m = 0, n = 0; // The walking indices to compare if two elements are the same
for (int i = 1; i < s2.length; i++) {
if (i > r) {
p[i] = 0;
m = i - 1;
n = i + 1;
} else {
int i2 = c * 2 - i;
if (p[i2] < (r - i)) {
p[i] = p[i2];
m = -1; // This signals bypassing the while loop below.
} else {
p[i] = r - i;
n = r + 1;
m = i * 2 - n;
}
}
while (m >= 0 && n < s2.length && s2[m] == s2[n]) {
p[i]++;
m--;
n++;
}
if ((i + p[i]) > r) {
c = i;
r = i + p[i];
}
}
int len = 0;
c = 0;
for (int i = 1; i < s2.length; i++) {
if (len < p[i]) {
len = p[i];
c = i;
}
}
char[] ss = Arrays.copyOfRange(s2, c - len, c + len + 1);
return String.valueOf(removeBoundaries(ss));
}

private static char[] addBoundaries(char[] cs) {
if (cs == null || cs.length == 0) {
return "||".toCharArray();
}

char[] cs2 = new char[cs.length * 2 + 1];
for (int i = 0; i < (cs2.length - 1); i = i + 2) {
cs2[i] = '|';
cs2[i + 1] = cs[i / 2];
}
cs2[cs2.length - 1] = '|';
return cs2;
}

private static char[] removeBoundaries(char[] cs) {
if (cs == null || cs.length < 3) {
return "".toCharArray();
}

char[] cs2 = new char[(cs.length - 1) / 2];
for (int i = 0; i < cs2.length; i++) {
cs2[i] = cs[i * 2 + 1];
}
return cs2;
}
}

class Palindrom {

int startPosition;
int length;
String palindrom;

Palindrom(int sp, String text) {
this.length = text.length();
this.palindrom = text;
this.startPosition = sp;
System.out.println("New palindrom at:" + sp);
}

public int getStarPosition() {
return startPosition;
}

public int getLength() {
return length;
}

public String getPalindrom() {
return palindrom;
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.