Advertisement
Guest User

9. Kamino Factory

a guest
May 16th, 2020
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class KaminoFactory {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. int length = Integer.parseInt(scan.nextLine());
  8. String sequenceDNA = "";
  9. int currentfirstindex = 0;
  10. String input = scan.nextLine();
  11. int bestLine = 0;
  12. int currentNumberLine = 0;
  13. int bestSubs1 = Integer.MIN_VALUE;
  14. int needIndexInLine = -1;
  15. int greaterSum = 0;
  16. int lowIndex = -1;
  17.  
  18. while (!"Clone them!".equals(input)) {
  19. currentNumberLine++;
  20.  
  21. String[] sequence = input.split("!+");
  22. int[] numberSequence = new int[length];
  23. for (int i = 0; i < numberSequence.length; i++) {
  24. numberSequence[i] = Integer.parseInt(sequence[i]);
  25. }
  26.  
  27. int currentCntsubs1 = 0;
  28. int currentSumLine = 0;
  29. int longestsubs1inLine = 0;
  30.  
  31. for (int i = 0; i < numberSequence.length; i++) {
  32. int cureentN = numberSequence[i];
  33. currentSumLine += cureentN;
  34. if (cureentN == 1) {
  35. currentCntsubs1++;
  36. currentfirstindex = i - currentCntsubs1 + 1;
  37. } else {
  38. currentCntsubs1 = 0;
  39. }
  40. if (currentCntsubs1 > longestsubs1inLine) {
  41. longestsubs1inLine = currentCntsubs1;
  42. needIndexInLine = currentfirstindex;
  43. }
  44. }
  45. if (longestsubs1inLine > bestSubs1) {
  46. sequenceDNA = "";
  47. bestSubs1 = longestsubs1inLine;
  48. bestLine = currentNumberLine;
  49. lowIndex = needIndexInLine;
  50. greaterSum = currentSumLine;
  51. for (int i = 0; i < length; i++) {
  52. sequenceDNA += numberSequence[i] + " ";
  53. }
  54. } else if (longestsubs1inLine == bestSubs1) {
  55. if (needIndexInLine < lowIndex) {
  56. sequenceDNA = "";
  57. bestSubs1 = longestsubs1inLine;
  58. bestLine = currentNumberLine;
  59. lowIndex = needIndexInLine;
  60. greaterSum = currentSumLine;
  61. for (int i = 0; i < length; i++) {
  62. sequenceDNA += numberSequence[i] + " ";
  63. }
  64. } else if (needIndexInLine == lowIndex) {
  65. if (currentSumLine > greaterSum) {
  66. sequenceDNA = "";
  67. bestSubs1 = longestsubs1inLine;
  68. bestLine = currentNumberLine;
  69. lowIndex = needIndexInLine;
  70. greaterSum = currentSumLine;
  71. for (int i = 0; i < length; i++) {
  72. sequenceDNA += numberSequence[i] + " ";
  73. }
  74. }
  75. }
  76. }
  77. input = scan.nextLine();
  78. }
  79. System.out.println(String.format("Best DNA sample %d with sum: %d.", bestLine, greaterSum));
  80. System.out.println(sequenceDNA);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement