Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- /*
- Let n, k be two integers and C_1,...,C_m a number of letters (alphabet), all given as a command line arguments.
- Validate the arguments!
- Create an array of n Strings (called words), each word containing exactly k characters from the given alphabet.
- Display on the screen the generated array.
- Two words are adjacent (neighbors) if they have a common letter.
- Create a boolean n x n matrix, representing the adjacency relation of the words. Display the matrix on the screen.
- Display the words that have the most, respectively the least, number of neighbors.
- Check if all words have the same number of neighbors.
- For larger n display the running time of the application in nanoseconds (DO NOT display the matrix!).
- Try n > 30_000. You might want to adjust the JVM Heap Space using the VM options -Xms4G -Xmx4G.
- Launch the application from the command line, for example: java Lab1 100 7 A C G T.
- */
- import java.util.Random;
- public class Main {
- public static boolean testStringArrays(String string1, String string2){
- for ( int i = 0 ; i < string1.length(); i++ ){
- for ( int j = 0 ; j < string2.length(); j++){
- if ( string1.charAt(i) == string2.charAt(j)) {
- return true;
- }
- }
- }
- return false;
- }
- public static void main(String[] args) {
- long startTime = System.nanoTime();
- int n = 0, k = 0;
- try{
- n = Integer.parseInt(args[0]);
- k = Integer.parseInt(args[1]);
- }
- catch (Exception e) {
- System.out.println("Exceptie aparuta la verificarea argumentelor: " + e);
- }
- String[] words = new String[n];
- for(int i = 0; i < words.length; i ++) {
- StringBuilder word = new StringBuilder();
- for(int j = 0; j < k; j ++){
- int random_number = new Random().nextInt(args.length - 2) + 2;
- word.append(args[random_number]);
- }
- words[i] = word.toString();
- }
- boolean[][] matrix = new boolean[n][n];
- for(int i = 0; i < words.length; i ++) {
- for(int j = 0; j < words.length; j ++) {
- if (testStringArrays(words[i], words[j])){
- matrix[i][j] = true;
- }
- else {
- matrix[i][j] = false;
- }
- }
- }
- int max = 0;
- int min = n;
- int idx_max = 0;
- int idx_min = 0;
- int count_true;
- for ( int i = 0 ; i < matrix.length; i++ ) {
- count_true = 0;
- for (int j = 0; j < matrix.length; j++) {
- if (matrix[i][j])
- count_true += 1;
- }
- if ( max < count_true ){
- max = count_true;
- idx_max = i;
- }
- if ( min > count_true ){
- min = count_true;
- idx_min = i;
- }
- }
- System.out.println("Cuvantul cu cei mai multi vecini: " + words[idx_max]);
- System.out.println("Cuvantul cu cei mai putini vecini: " + words[idx_min]);
- int count_true_first_word = 0;
- for (int j = 0; j < matrix.length; j++) {
- if (matrix[0][j])
- count_true_first_word += 1;
- }
- boolean same_number_of_neighbours = true;
- for ( int i = 0 ; i < matrix.length; i++ ) {
- count_true = 0;
- for (int j = 0; j < matrix.length; j++) {
- if (matrix[i][j])
- count_true += 1;
- }
- if (count_true_first_word != count_true)
- {
- same_number_of_neighbours = false;
- break;
- }
- }
- System.out.println("Toate cuvintele au acelasi numar de vecini: " + same_number_of_neighbours);
- long endTime = System.nanoTime();
- long totalTime = endTime - startTime;
- System.out.println("Timpul total de rulare pentru n = " + n + " este: " + totalTime);
- /*
- System.out.println("Afisarea matricii: ");
- for ( int i = 0 ; i < matrix.length; i++ ){
- for ( int j = 0 ; j < matrix.length; j ++ ){
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement