I'm reading and learning about a Java implementation of radix sort, as shown below. It would be great if someone could clarify the logical meaning of pointTo, index and globalPtr.
https://www.hackerrank.com/challenges/string-similarity/editorial
private void radixSort0() {
globalPtr = 0;
Arrays.fill(bucketHead, -1);
Arrays.fill(next, -1);
for (int i = 0; i < n; i++) {
int value = nr0[index[i]];
if (bucketHead[value] == -1) bucketHead[value] = bucketTail[value] = globalPtr;
else bucketTail[value] = next[bucketTail[value]] = globalPtr;
pointTo[globalPtr++] = index[i];
}
int ptr = 0;
for (int i = 0; i < M; i++)
for (int j = bucketHead[i]; j != -1; j = next[j])
index[ptr++] = pointTo[j];
}
This radixSort0() is not a complete radix sort. If your goal is to learn about radix sort, look elsewhere.
In both (needlessly duplicated) radixSort methods, int[] next is used to establish singly linked lists - using indexes instead of references, and -1 instead of null. (You can not just set next[some_index_depending_on value] to index[i] - there would be no lists.) The int[] pointTo would probably be more descriptively be named value. Think of next&value as linked lists, represented in an instance with two data members of type array, as an alternative to an array of instances with members next&value. globalPtr is the smallest index not yet allocated in that/those array/s.
(The blaring lack of comments in the code to follow is owing to my lack of understanding why anyone should try and construct a suffix array using this, or what the pieces of code contribute to that goal: feel free to correct&amend.)
Not even thinking about testing, the Java way of handling this might be
private void radixSortStep(int[]nr) {
List<Integer> value[] = new List[M];
for (int i = 0; i < value.length; i++)
value[i] = new ArrayList<Integer>(0);
for (int i: indexes)
value[nr[i]].add(i);
int ptr = 0;
for (int i = 0; i < M; i++)
for (int val: value[i])
indexes[ptr++] = val;
}
(with a bit of hand-waving about M (set to n+1) and nr1 (initialise entries not copied from rank to n, not -1))
import java.io.*;
import java.util.*;
public class St {
public static int calculate(String s){
char[]arr=s.toCharArray();
int length=arr.length;
int count=length;
for(int i=1;i<length;i++){
int len=length-i;
int j=0;
for(;j<len;j++)
if(arr[j]!=arr[j+i]){
break;
}
count+=j;
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner( System.in );
int n=scanner.nextInt();
for(int i=0;i<n;i++){
String s=scanner.next();
System.out.println(calculate(s));
}
}
}
it almost passed all testcases except last two due to timeout hope my work helps happy coding..
Related
I wanted to create two-dimensional matrixes on an automated routine (depending on inputs) and then make them return to see the results.
Here's my code:
public class Two_dimensional_arrays {
static int[][] array1;
public static int[][] create_array(int number1, int number2) {
int k = 1;
for (int j = 0; j <= number2; j++) {
for (int i = 0; i <= number1; i++) {
array[i][j] = k;
k++;
}
}
return array1;
}
}
The logic behind all of that was to fill any matrix in order like (let's make a 3x3 one)
1-2-3
4-5-6
7-8-9
So the matrix would be automatically filled (in ascending order), but it didn't work out as expected since I'm kind of new to programming.
You have to actually allocate the array somewhere.
static int[][] array1;
This declares a variable capable of holding a reference to an array; it is not the array. The array is allocated by 'new':
static int[][] array1 = new int[3][3];
A couple of other points:
The 'array' in your loop should probably be 'array1'.
Since 'array1' is a member variable there's probably no point in returning it as well. You could go either way: have a single array, as at present, and your create_array method will overwrite it each time; OR have create_array allocate an array of the intended size (new int[number1][number2]) and return it. I'd go for the latter.
The arguments could be named better: 'row_count' rather than 'number1', 'column_count' rather than 'number2' -- or anything similar that conveys the intended meaning. (Typically Java programmers use camel-case names, like rowCount, rather than underscore, but that's not a particularly interesting discussion and not my point here).
public class Two_dimensional_arrays {
public static int[][] create_array(int number1, int number2){
int[][] matrix = new int[number1][number2];
int k = 1;
for(int i = 0; i<number1; i++){
for(int j = 0; j<number2; j++){
matrix[i][j] = k;
k++;
}
}
return matrix;
}
}
I'm trying to sort an ArrayList<> of names using selection sort to sort names alphabetically in both increasing and descending order using my two methods minimumPosition() and maximumPosition(). I'm having trouble comparing the Strings because it says that .compareToIgnoreCase requires an array but an ArrayList<String> was found. I'm not sure of any other way to compare an ArrayList<> to print out my names in both alphabetical orders.
I've tried switching my return method type from int to String but that didn't help, I've also tried switching some of the variable types from int to String just to see if anything would work and it did not. I've searched around the web to find a solution to this, but I've only found how to use selection sort with integers and arrays, not ArrayLists. The ArrayList<> of integers in my code is for the second part of my assignment dealing with linear and binary search, so ignore that. My main focus is with the ArrayList<> of names. Any help would be appreciated, below is a sample of my code.
package classwork_additional1;
import java.util.ArrayList;
import java.util.Collections;
public class Classwork_Additional1 {
public static void main(String[] args){
ArrayList<String> array = new ArrayList<>();
array.add("Mary");
array.add("Mike");
array.add("Michael");
array.add("Ann");
array.add("Tom");
array.add("Tommy");
array.add("Bob");
ArrayList<Integer> integers = new ArrayList<>();
integers.add(10);
integers.add(10);
integers.add(5);
integers.add(8);
integers.add(10);
integers.add(7);
integers.add(10);
}
public static void sort(ArrayList<String> array)
{
for (int i = 0; i < array.size() - 1; i++)
{
int minPos = minimumPosition(array, i);
swap(array, minPos, i);
}
}
public static void swap(ArrayList<String> array, int i, int j)
{
String temp = array.get(i);
array.set(i, array.get(j));
array.set(j, temp);
}
private static int minimumPosition(ArrayList<String> array, int from){
int minPos = from;
for (int i = from + 1; i < array.size(); i++)
{
if (array.get(i).compareToIgnoreCase(array[minPos]) < 0)
minPos = i;
}
return minPos;
}
private static int maximumPosition(ArrayList<String> array, int from)
{
int maxPos = from;
for (int i = from + 1; i < array.size(); i++)
{
if (array.get(i).compareToIgnoreCase(array[maxPos]) > 0)
maxPos = i;
}
return maxPos;
}
I'm expecting the names to appear in alphabetical order starting with "Ann" and going up to "Tommy" and also on another line going from "Tommy" going back down to "Ann".
Simply change
if(array.get(i).compareToIgnoreCase(array[minPos]) < 0)
to
if(array.get(i).compareToIgnoreCase(array.get(minPos)) < 0)
If you are trying to get the element at one specific index two times, why are you trying it with differend approches?
I found this code online and it works well to permute through the given array and return all possible combinations of the numbers given. Does anyone know how to change this code to incorporate a 2D array instead?
public static ArrayList<ArrayList<Integer>> permute(int[] numbers) {
ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
permutations.add(new ArrayList<Integer>());
for ( int i = 0; i < numbers.length; i++ ) {
ArrayList<ArrayList<Integer>> current = new ArrayList<ArrayList<Integer>>();
for ( ArrayList<Integer> p : permutations ) {
for ( int j = 0, n = p.size() + 1; j < n; j++ ) {
ArrayList<Integer> temp = new ArrayList<Integer>(p);
temp.add(j, numbers[i]);
current.add(temp);
}
}
permutations = new ArrayList<ArrayList<Integer>>(current);
}
return permutations;
}
This is what I have attempted:
public static int[][] permute(int[] numbers){
int[][] permutations = new int[24][4];
permutations[0] = new int[4];
for ( int i = 0; i < numbers.length; i++ ) {
int[][] current = new int[24][4];
for ( int[] permutation : permutations ) {
for ( int j = 0; j < permutation.length; j++ ) {
permutation[j] = numbers[i];
int[] temp = new int[4];
current[i] = temp;
}
}
permutations = current;
}
return permutations;
}
However this returns all zeroes. I chose 24 and 4 because that is the size of the 2D array that I need.
Thanks
It’s not really that easy. The original code exploits the more dynamic behaviour of ArrayList, so a bit of hand coding will be necessary. There are many correct thoughts in your code. I tried to write an explanation of the issues I saw, but it became too long, so I decided to modify your code instead.
The original temp.add(j, numbers[i]); is the hardest part to do with arrays since it invloves pushing the elements to the right of position j one position to the right. In my version I create a temp array just once in the middle loop and shuffle one element at a time in the innermost loop.
public static int[][] permute(int[] numbers) {
// Follow the original here and create an array of just 1 array of length 0
int[][] permutations = new int[1][0];
for (int i = 0; i < numbers.length; i++) {
// insert numbers[i] into each possible position in each array already in permutations.
// create array with enough room: when before we had permutations.length arrays, we will now need:
int[][] current = new int[(permutations[0].length + 1) * permutations.length][];
int count = 0; // number of new permutations in current
for (int[] permutation : permutations) {
// insert numbers[i] into each of the permutation.length + 1 possible positions of permutation.
// to avoid too much shuffling, create a temp array
// and use it for all new permutations made from permutation.
int[] temp = Arrays.copyOf(permutation, permutation.length + 1);
for (int j = permutation.length; j > 0; j--) {
temp[j] = numbers[i];
// remember to make a copy of the temp array
current[count] = temp.clone();
count++;
// move element to make room for numbers[i] at next position to the left
temp[j] = temp[j - 1];
}
temp[0] = numbers[i];
current[count] = temp.clone();
count++;
}
assert count == current.length : "" + count + " != " + current.length;
permutations = current;
}
return permutations;
}
My trick with the temp array means I don’t get the permutations in the same order as in the origianl code. If this is a requirement, you may copy permutation into temp starting at index 1 and shuffle the opposite way in the loop. System.arraycopy() may do the initial copying.
The problem here is that you really need to implement properly the array version of the ArrayList.add(int,value) command. Which is to say you do an System.arraycopy() and push all the values after j, down one and then insert the value at j. You currently set the value. But, that overwrites the value of permutation[j], which should actually have been moved to permutations[j+1] already.
So where you do:
permutation[j] = numbers[i];
It should be:
System.arraycopy(permutation,j, permutations, j+1, permutations.length -j);
permutation[j] = numbers[i];
As the ArrayList.add(int,value) does that. You basically wrongly implemented it as .set().
Though personally I would scrap the code and go with something to dynamically make those values on the fly. A few more values and you're talking something prohibitive with regard to memory. It isn't hard to find the nth index of a permutation. Even without allocating any memory at all. (though you need a copy of the array if you're going to fiddle with such things without incurring oddities).
public static int[] permute(int[] values, long index) {
int[] returnvalues = Arrays.copyOf(values,values.length);
if (permutation(returnvalues, index)) return returnvalues;
else return null;
}
public static boolean permutation(int[] values, long index) {
return permutation(values, values.length, index);
}
private static boolean permutation(int[] values, int n, long index) {
if ((index == 0) || (n == 0)) return (index == 0);
int v = n-(int)(index % n);
int temp = values[n];
values[n] = values[v];
values[v] = temp;
return permutation(values,n-1,index/n);
}
This seems simple enough but I get the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at reverse.main(reverse.java:28)"
I initially take inputs from the user to write an array, and then I want to print the array backwards. I understand there are other ways of doing this, but I mainly want to know why this is not working. Going through it line by line makes sense?
PS. If it's not a problem, is there any better way of doing this?
import java.util.Scanner;
public class reverse {
/**
* #param args
*/
public static void main(String[] args) {
System.out.printf("Enter the number of values in array: ");
Scanner scanner = new Scanner(System.in);
int n;
n = scanner.nextInt();
double[] a1 = new double[n];
int i;
System.out.printf("Enter the value in the array: ");
for (i = 0; i < n; i++){
Scanner scanner2 = new Scanner(System.in);
a1[i] = scanner2.nextInt();
}
double j;
double k;
for (i = 0; i < n/2; i++){
j = a1[i];
k = a1[n-i]; //error line;
a1[i]=k;
a1[n-i]=j;
}
for(i = 0; i < n; i++){
System.out.println(" "+a1[i]);
}}
}
When i = 0, n-i will result in n, which is one larger than the available indexes( 0 -> n-1 ).
for (i = 0; i < n/2; i++){
j = a1[i];
k = a1[n-i]; //error line;
a1[i]=k;
a1[n-i]=j;
}
Collections.reverse(Arrays.asList(array))
Will reverse an array for you, then just print its values out. It's great to do these kinds of problems as exercises but if you're ever woring in the industry it's usually better to rely on the Java API for trivial things like this. Probably going to be faster and a lot more simpler than anything you can come up with.
As said by Samhain, when i = 0, then n-i == n, which is greater than the last index of the array (since arrays start with index 0).
The simplest solution is to just subtract an additional 1 from n-i.
j = a1[i];
k = a1[n-i-1];
a1[i]=k;
a1[n-i-1]=j;
Also, creating a new Scanner is totally unnecessary. Just continue to use the first one you created.
for (i = 0; i < n; i++){
a1[i] = scanner.nextInt();
}
Finally, for what it's worth, if you're using nextInt you don't need to declare your array as a double[] (nor do j and k need to be doubles). You can just use ints.
Here's it running on ideone.
Need java function to find the longest duplicate substring in a string
For instance, if the input is “banana”,output should be "ana" and we have count the number of times it has appeared in this case it is 2.
The solution is as below
public class Test{
public static void main(String[] args){
System.out.println(findLongestSubstring("i like ike"));
System.out.println(findLongestSubstring("madam i'm adam"));
System.out.println(findLongestSubstring("When life hands you lemonade, make lemons"));
System.out.println(findLongestSubstring("banana"));
}
public static String findLongestSubstring(String value) {
String[] strings = new String[value.length()];
String longestSub = "";
//strip off a character, add new string to array
for(int i = 0; i < value.length(); i++){
strings[i] = new String(value.substring(i));
}
//debug/visualization
//before sort
for(int i = 0; i < strings.length; i++){
System.out.println(strings[i]);
}
Arrays.sort(strings);
System.out.println();
//debug/visualization
//after sort
for(int i = 0; i < strings.length; i++){
System.out.println(strings[i]);
}
Vector<String> possibles = new Vector<String>();
String temp = "";
int curLength = 0, longestSoFar = 0;
/*
* now that the array is sorted compare the letters
* of the current index to those above, continue until
* you no longer have a match, check length and add
* it to the vector of possibilities
*/
for(int i = 1; i < strings.length; i++){
for(int j = 0; j < strings[i-1].length(); j++){
if (strings[i-1].charAt(j) != strings[i].charAt(j)){
break;
}
else{
temp += strings[i-1].charAt(j);
curLength++;
}
}
//this could alleviate the need for a vector
//since only the first and subsequent longest
//would be added; vector kept for simplicity
if (curLength >= longestSoFar){
longestSoFar = curLength;
possibles.add(temp);
}
temp = "";
curLength = 0;
}
System.out.println("Longest string length from possibles: " + longestSoFar);
//iterate through the vector to find the longest one
int max = 0;
for(int i = 0; i < possibles.size();i++){
//debug/visualization
System.out.println(possibles.elementAt(i));
if (possibles.elementAt(i).length() > max){
max = possibles.elementAt(i).length();
longestSub = possibles.elementAt(i);
}
}
System.out.println();
//concerned with whitespace up until this point
// "lemon" not " lemon" for example
return longestSub.trim();
}
}
This is a common CS problem with a dynamic programming solution.
Edit (for lijie):
You are technically correct -- this is not the exact same problem. However this does not make the link above irrelevant and the same approach (w.r.t. dynamic programming in particular) can be used if both strings provided are the same -- only one modification needs to be made: don't consider the case along the diagonal. Or, as others have pointed out (e.g. LaGrandMere), use a suffix tree (also found in the above link).
Edit (for Deepak):
A Java implementation of the Longest Common Substring (using dynamic programming) can be found here. Note that you will need to modify it to ignore "the diagonal" (look at the Wikipedia diagram) or the longest common string will be itself!
In Java : Suffix Tree.
Thanks to the ones that have found how to solve it, I didn't know.