Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Driver
- {
- public static void main(String[] args)
- {
- try
- {
- //scanning the n bower sources and the n LEDs from a file
- Scanner scanner = new Scanner(new File("dynamicfile.txt"));
- //array called num that holds the number in the first line and the second line
- int []num = new int[2];
- //i is a counter
- int i=0;
- while (scanner.hasNextInt())
- {
- num[i]=scanner.nextInt();
- i++;
- }
- //count is to count number of digits in each integer value
- int count=0;
- //since we have two different values, counts is to hold these different values
- int []counts=new int[2];
- //since we are gonna change the value of num so we can get the number of digit, we hold them in a different type of integer
- int num1=num[0];
- int num2=num[1];
- //calculating the number of digits in each integer
- for(i=0;i<2;i++)
- {
- count=0;
- while(num[i] != 0 )
- {
- num[i] /= 10;
- ++count;
- counts[i]=count;
- }
- }
- //creating arrays that will hold the n (n=count[0]) power sources, and the n LEDs(n=count[1] here)
- int []S=new int[counts[0]];
- int []L=new int[counts[1]];
- //splitting the integer into individual digits and saving them in the power source array
- while (num1 > 0 )
- {
- for(int b=counts[0]-1;b>=0;b--)
- {
- S[b]= num1 % 10;
- num1 = num1/ 10;
- }
- }
- //splitting the integer into individual digits and saving them in the LEDs array
- while (num2 > 0 )
- {
- for(int b=counts[1]-1;b>=0;b--)
- {
- L[b]= num2 % 10;
- num2 = num2/ 10;
- }
- }
- //QuickSort quick=new QuickSort();
- //quick.quickSort(S, 0,counts[0]-1);
- MaxNumofLEDs max=new MaxNumofLEDs();
- int maxnum=max.MaxNum(S,L,counts[0],counts[1]);
- System.out.println(maxnum);
- for(int b=0;b<counts[0];b++)
- System.out.println(L[b]);
- /*int size=maxnum;
- int m=S.length;
- int n=L.length;
- int []l= new int[size+1];
- l[size]=0;
- while(m>0 && n>0)
- {
- if(S[m-1]==L[n-1])
- {
- l[--size]=S[m-1];
- m--;
- n--;
- }
- else if(S[m-1]>L[n-1])
- {
- m--;
- }
- else
- n--;
- }
- System.out.println(l);*/
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- }
- public class MaxNumofLEDs {
- public int MaxNum( int[] S, int[] L, int m, int n )
- {
- //max holds the number of leds that are connected with the power source
- int max[][] = new int[m+1][n+1];
- //the initial value if one of the circuts is empty is zero
- for(int i=0;i<=m;i++)
- {
- max[i][0]=0;
- }
- for(int j=0;j<=n;j++)
- {
- max[0][j]=0;
- }
- for (int i=1; i<=m; i++)
- {
- for (int j=1; j<=n; j++)
- {
- if (S[i-1] == L[j-1])
- max[i][j] = max[i-1][j-1] + 1;
- else
- max[i][j] = Math.max(max[i-1][j], max[i][j-1]);
- }
- }
- for (int k = 0; k <= S.length; k++) {
- for (int j = 0; j <= L.length; j++) {
- System.out.print(max[k][j]+" ");
- }
- System.out.println("");
- }
- //returns the maximum number of LEDs
- return max[m][n];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement