Você está na página 1de 3

package DataStructuresAndAlgorithm;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;

/**
* The program prints the minimum cuts required to make each substring in a string
a palindrome.
* This version uses the concept of memorization.
*
* Input :
* The txt file is required at the directory :
D:\Programming\JAVA\Code\Coding\DataStructuresAndAlgorithm\Input\MinimumCuts.txt
* The acticle about input is written/updated on 20th August 2019.
*
* @author Tushar Singh
* @version
* 1.0 created on 20th July 2018
*/

public class MinimumCuts{


private MinimumCuts(){
}
private static class Structure{
private Block firstBlock;
private Structure(){
}
public Structure(String str){
Block [] block = new Block[str.length()];
setBlock(block);
f(block , str);
setFirstBlock(block[0]);
}
private void setFirstBlock(Block firstBlock){
this.firstBlock = firstBlock;
}
private void f(Node [] block , String str){
int i = 0;
while(i != str.length()){
setNode( block , str , i - 1 , i + 1);
setNode( block , str , i - 1 , i++);
}
}
private void setNode(Node [] block , String str , int left , int right){
while(left != -1 && right != str.length()){
if(str.charAt(left) != str.charAt(right)){
return;
}
Node fresh = new Node();
fresh.end = block[right].end;
fresh.next = block[left].next;
block[left].next = fresh;
--left;
++right;
}
}
private void setBlock(Node [] block){
int i;
for(i = 0 ; i != block.length ; ++i){
block[i] = new Block();
}
for(i = 1 ; i != block.length ; ++i){
block[i - 1].end = block[i];
}
}
private class Node{
private Node end;
private Node next;
public void setEnd(Node end){
this.end = end;
}
}
private class Block extends Node{
private int minimumCuts;
}
}
/**
* The execution method.
*/
public static void main(String [] args) throws IOException{
BufferedReader br = new BufferedReader(new
FileReader("D:\\Programming\\JAVA\\Code\\Coding\\DataStructuresAndAlgorithm\\Input\
\MinimumCuts.txt"));
String str;
StringBuffer sb = new StringBuffer();
long start , end;
String CONSTANT1 = "Input : " , CONSTANT2 = "Number of substrings : " ,
CONSTANT3 = "Time taken in ns : " , CONSTANT4 = "Time taken in ms : " ,
CONSTANT5 = "Time taken in s : " , CONSTANT6 = "Length : ";
while((str = br.readLine()) != null){
str = str.toLowerCase();
start = System.nanoTime();
sb.append(CONSTANT1 + str + "\n");
sb.append(CONSTANT6 + str.length() + "\n");
sb.append(CONSTANT2 + print(str));
end = System.nanoTime() - start;
sb.append(CONSTANT3 + String.valueOf(end) + "\n");
sb.append(end / 1_000_000L == 0 ? "" : CONSTANT4 + String.valueOf(end /
1_000_000L) + "\n");
sb.append(end / 1_000_000_000L == 0 ? "" : CONSTANT4 +
String.valueOf(end / 1_000_000_000L) + "\n");
sb.append("\n");
}
System.out.print(sb);
}
private static String print(String str){
Structure s = new Structure(str);
setMinimumCuts(s.firstBlock);
return String.valueOf(s.firstBlock.minimumCuts) + "\n";
}
private static void setMinimumCuts(Structure.Node n){
if(((Structure.Block)n).minimumCuts != 0){
return ;
}
if(n.end == null){
return;
}
Structure.Block block = (Structure.Block)n;
setMinimumCuts(n.end);
block.minimumCuts = ((Structure.Block)(n.end)).minimumCuts;
for(n = n.next ; n != null ; n =n.next){
if(n.end == null){

block.minimumCuts = 0;
return;
}
setMinimumCuts(n.end);
if(((Structure.Block)(n.end)).minimumCuts < block.minimumCuts){
block.minimumCuts = ((Structure.Block)(n.end)).minimumCuts;
}
}
block.minimumCuts++;
}
}

Você também pode gostar