junbjn98

asdadasd

Jul 14th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function splitString(s){
  2.     let arr = [];
  3.     let str = ``;
  4.     for (i in s) {
  5.         if (isNaN(s[i])) {
  6.             str += s[i];
  7.             arr.push(str);
  8.             str = "";
  9.         } else {
  10.             str += s[i];
  11.         }
  12.     }
  13.     return arr;
  14. }
  15.  
  16. checkSort = (a,b) => {
  17.     if (a.length > b.length) return 1;
  18.     if (a.length < b.length) return -1;
  19.     const tempA = a.substr(0, a.length - 1);
  20.     const tempB = b.substr(0, b.length - 1);
  21.     if (Number(tempA) > Number(tempB)) return 1;
  22.     if (Number(tempA) < Number(tempB)) return -1;
  23.     if (a[a.length - 1] > b[b.length - 1]) return 1;
  24.     if (a[a.length - 1] < b[b.length - 1]) return -1;
  25.     return 0;
  26. }
  27.  
  28. countPhomTuQuy = (a) => {
  29.     let count = 0;
  30.     let num0 = a[0].substr(0, a[0].length - 1);
  31.     let num1 = a[1].substr(0, a[1].length - 1);
  32.     let num2 = a[2].substr(0, a[2].length - 1);
  33.     let num3 = a[3].substr(0, a[3].length - 1);
  34.     let num4 = a[4].substr(0, a[4].length - 1);
  35.  
  36.     let countTu = 0;
  37.     let countSa = 0;
  38.     if (num1 != num0) countTu++;
  39.     if (num2 != num0) countTu++;
  40.     if (num3 != num0) countTu++;
  41.  
  42.     if (a[0][a[0].length - 1] != a[1][a[1].length - 1] || Number(num0) + 1 != Number(num1)) countSa++;
  43.     if (a[0][a[0].length - 1] != a[2][a[2].length - 1] || Number(num0) + 2 != Number(num2)) countSa++;
  44.     if (a[0][a[0].length - 1] != a[3][a[3].length - 1] || Number(num0) + 3 != Number(num3)) countSa++;
  45.    
  46.     count += countTu > countSa ? countSa : countTu;
  47.  
  48.     for (let i = 5; i < 9; i++) {
  49.         if (a[i][a[i].length - 1] !== a[4][a[4].length - 1]) {
  50.             count++;
  51.             continue;
  52.         }
  53.  
  54.         let numi = a[i].substr(0, a[i].length - 1);
  55.         if (Number(numi) != Number(num4) + i - 4) count++;
  56.     }
  57.  
  58.     return count;
  59. }
  60.  
  61. coutNumSam = (a) => {
  62.     let count = 0;
  63.     for(let i = 0; i < 3; i++) {
  64.         let c1 = a[i *3];
  65.         let c2 = a[i * 3 + 1];
  66.         let c3 = a[i * 3 + 2];
  67.  
  68.         let num1 = c1.substr(0, c1.length - 1);
  69.         let num2 = c2.substr(0, c2.length - 1);
  70.         let num3 = c3.substr(0, c3.length - 1);
  71.  
  72.         countSam = 2;
  73.         countSanh = 0;
  74.  
  75.         if (num1 == num2 && num2 == num3) {
  76.             countSam = 0;
  77.         } else if (num1 != num2 && num1 != num3 && num2 != num3) {
  78.             countSam = 2;
  79.         } else {
  80.             countSam = 1;
  81.         }
  82.  
  83.         if (Number(num1) > 11) {
  84.             countSanh = 3;
  85.         } else {
  86.             if (c1[c1.length - 1] !== c2[c2.length - 1]) {
  87.                 countSanh++;
  88.             } else if (Number(num2) - Number(num1) != 1) {
  89.                 countSanh++;
  90.             }
  91.  
  92.             if (c1[c1.length - 1] !== c3[c3.length - 1]) {
  93.                 countSanh++;
  94.             } else if (Number(num3) - Number(num1) != 2) {
  95.                 countSanh++;
  96.             }
  97.         }
  98.  
  99.         count += countSam > countSanh ? countSanh : countSam;
  100.     }
  101.  
  102.     return count;
  103. }
  104.  
  105. countChange = (arr) => {
  106.     let count1 = countPhomTuQuy(arr);
  107.     let count2 = coutNumSam(arr);
  108.  
  109.     return count1 > count2 ? count2 : count1;
  110. }
  111.  
  112. generate = (a) => {
  113.     let min = 9;
  114.     while(true) {
  115.         let i = a.length - 2;
  116.         let temp = 0;
  117.         while(i>=0&& checkSort(a[i],a[i+1]) == 1 ) i--;
  118.         if(i < 0) return min; //hoán vị cuối
  119.         else
  120.         {
  121.             let k = a.length - 1; //tim so nho nhat tu i tro di
  122.             while(checkSort(a[k],a[i]) == -1) k--;
  123.  
  124.             //hoán vị a[i] với phần tử nhỏ nhất lớn hơn a[i]
  125.             temp = a[k];
  126.             a[k] = a[i];
  127.             a[i] = temp;
  128.  
  129.             //đảo ngược dãy từ i+1 đến n-1
  130.             let e=a.length-1,b=i+1;
  131.             while(e>b)
  132.             {
  133.                 temp = a[e];
  134.                 a[e] = a[b];
  135.                 a[b] = temp;
  136.                 b++;e--;
  137.             }
  138.         }
  139.         temp = countChange(a);
  140.         if (min > temp) min = temp;
  141.     }
  142. }
  143.  
  144. function xxxxxxx(s){
  145.     let arr = splitString(s);
  146.     arr = arr.sort(checkSort);
  147.     let min = countChange(arr);
  148.     let temp = generate(arr);
  149.     if (min > temp) min = temp;
  150.  
  151.  
  152.     return min;  
  153. }
Add Comment
Please, Sign In to add comment