Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.LinkedList;
  3.  
  4.  
  5. public class GuitarChords {
  6. public int stretch(String[] strings, String[] chord) {
  7. int[] str = new int[strings.length];
  8. int[] chd = new int[chord.length];
  9. for (int i = 0; i < str.length; i++) {
  10. str[i] = noteToInt(strings[i]);
  11. }
  12. for (int i = 0; i < chd.length; i++) {
  13. chd[i] = noteToInt(chord[i]);
  14. }
  15.  
  16. return bestest(str, chd, new LinkedList<Integer>(), Integer.MAX_VALUE);
  17. }
  18.  
  19. private int bestest(int[] str, int[] chd, LinkedList<Integer> comb, int best) {
  20. if (comb.size() == str.length) {
  21. return Math.min(eval(str, chd, comb), best);
  22. }
  23. for (int i = 0; i < 12; i++) {
  24. comb.add(str[comb.size()] + i);
  25. best = Math.min(best, bestest(str, chd, comb, best));
  26. comb.removeLast();
  27. }
  28. return best;
  29. }
  30.  
  31. private int eval(int[] str, int[] chd, LinkedList<Integer> comb) {
  32. HashSet<Integer> left = new HashSet<Integer>();
  33. for (int i : chd) {
  34. left.add(i);
  35. }
  36. for (int i : comb) {
  37. if (left.contains(i)) {
  38. left.remove(i);
  39. } else {
  40. return Integer.MAX_VALUE;
  41. }
  42. }
  43. int min = Integer.MAX_VALUE;
  44. int max = Integer.MIN_VALUE;
  45. boolean fffuuu = true;
  46. for (int i = 0; i < str.length; i++) {
  47. int diff = comb.get(i) - str[i];
  48. if (diff != 0) {
  49. fffuuu = false;
  50. min = Math.min(min, diff);
  51. max = Math.max(max, diff);
  52. }
  53. }
  54. if (fffuuu) {
  55. return 0;
  56. }
  57. return max - min + 1;
  58. }
  59.  
  60. public int noteToInt(String note) {
  61. String[] foo = "A A# B C C# D D# E F F# G G#".split(" ");
  62. for (int i = 0; i < foo.length; i++) {
  63. if (foo[i].equals(note)) return i;
  64. }
  65. return -1;
  66. }
  67.  
  68. public static void main(String[] args) {
  69. System.out.println(new GuitarChords().stretch(
  70. "A C F".split(" "),
  71. "C# F# A#".split(" ")
  72. ));
  73. System.out.println(new GuitarChords().stretch(
  74. "E A D G B E".split(" "),
  75. "E G# B".split(" ")
  76. ));
  77. System.out.println(new GuitarChords().stretch(
  78. "D#".split(" "),
  79. "D#".split(" ")
  80. ));
  81. System.out.println(new GuitarChords().stretch(
  82. "E F".split(" "),
  83. "F# D#".split(" ")
  84. ));
  85. System.out.println(new GuitarChords().stretch(
  86. "C C C".split(" "),
  87. "C E G".split(" ")
  88. ));
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement