Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. package com.company;
  2.  
  3. /*
  4. Let n, k be two integers and C_1,...,C_m a number of letters (alphabet), all given as a command line arguments.
  5. Validate the arguments!
  6. Create an array of n Strings (called words), each word containing exactly k characters from the given alphabet.
  7. Display on the screen the generated array.
  8. Two words are adjacent (neighbors) if they have a common letter.
  9. Create a boolean n x n matrix, representing the adjacency relation of the words. Display the matrix on the screen.
  10. Display the words that have the most, respectively the least, number of neighbors.
  11. Check if all words have the same number of neighbors.
  12. For larger n display the running time of the application in nanoseconds (DO NOT display the matrix!).
  13. Try n > 30_000. You might want to adjust the JVM Heap Space using the VM options -Xms4G -Xmx4G.
  14. Launch the application from the command line, for example: java Lab1 100 7 A C G T.
  15. */
  16.  
  17. import java.util.Random;
  18.  
  19. public class Main {
  20.  
  21. public static boolean testStringArrays(String string1, String string2){
  22. for ( int i = 0 ; i < string1.length(); i++ ){
  23. for ( int j = 0 ; j < string2.length(); j++){
  24. if ( string1.charAt(i) == string2.charAt(j)) {
  25. return true;
  26. }
  27. }
  28. }
  29. return false;
  30. }
  31.  
  32. public static void main(String[] args) {
  33. long startTime = System.nanoTime();
  34.  
  35. int n = 0, k = 0;
  36. try{
  37. n = Integer.parseInt(args[0]);
  38. k = Integer.parseInt(args[1]);
  39. }
  40. catch (Exception e) {
  41. System.out.println("Exceptie aparuta la verificarea argumentelor: " + e);
  42. }
  43. String[] words = new String[n];
  44. for(int i = 0; i < words.length; i ++) {
  45. StringBuilder word = new StringBuilder();
  46. for(int j = 0; j < k; j ++){
  47. int random_number = new Random().nextInt(args.length - 2) + 2;
  48. word.append(args[random_number]);
  49. }
  50. words[i] = word.toString();
  51. }
  52. boolean[][] matrix = new boolean[n][n];
  53. for(int i = 0; i < words.length; i ++) {
  54. for(int j = 0; j < words.length; j ++) {
  55. if (testStringArrays(words[i], words[j])){
  56. matrix[i][j] = true;
  57. }
  58. else {
  59. matrix[i][j] = false;
  60. }
  61. }
  62. }
  63. int max = 0;
  64. int min = n;
  65. int idx_max = 0;
  66. int idx_min = 0;
  67. int count_true;
  68. for ( int i = 0 ; i < matrix.length; i++ ) {
  69. count_true = 0;
  70. for (int j = 0; j < matrix.length; j++) {
  71. if (matrix[i][j])
  72. count_true += 1;
  73. }
  74. if ( max < count_true ){
  75. max = count_true;
  76. idx_max = i;
  77. }
  78. if ( min > count_true ){
  79. min = count_true;
  80. idx_min = i;
  81. }
  82. }
  83. System.out.println("Cuvantul cu cei mai multi vecini: " + words[idx_max]);
  84. System.out.println("Cuvantul cu cei mai putini vecini: " + words[idx_min]);
  85.  
  86. int count_true_first_word = 0;
  87. for (int j = 0; j < matrix.length; j++) {
  88. if (matrix[0][j])
  89. count_true_first_word += 1;
  90. }
  91.  
  92. boolean same_number_of_neighbours = true;
  93.  
  94. for ( int i = 0 ; i < matrix.length; i++ ) {
  95. count_true = 0;
  96. for (int j = 0; j < matrix.length; j++) {
  97. if (matrix[i][j])
  98. count_true += 1;
  99. }
  100. if (count_true_first_word != count_true)
  101. {
  102. same_number_of_neighbours = false;
  103. break;
  104. }
  105. }
  106.  
  107. System.out.println("Toate cuvintele au acelasi numar de vecini: " + same_number_of_neighbours);
  108.  
  109. long endTime = System.nanoTime();
  110. long totalTime = endTime - startTime;
  111. System.out.println("Timpul total de rulare pentru n = " + n + " este: " + totalTime);
  112. /*
  113. System.out.println("Afisarea matricii: ");
  114. for ( int i = 0 ; i < matrix.length; i++ ){
  115. for ( int j = 0 ; j < matrix.length; j ++ ){
  116. System.out.print(matrix[i][j] + " ");
  117. }
  118. System.out.println();
  119. }
  120. */
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement