Advertisement
Guest User

Word Game 2

a guest
Feb 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function processData(input) {
  2.     var arr = input.split("\n");
  3.     var start = arr[0];
  4.     var target = arr[1];
  5.    
  6.     var longest = 0;
  7.     var L = new Array(start.length);
  8.     for(var i=0; i<start.length; i++){
  9.         L[i] = new Array(target.length);
  10.     }
  11.  
  12.     for(var i=0; i<L.length; i++){
  13.         for(var j=0; j<L[0].length; j++){
  14.             if(start.charAt(i)==target.charAt(j)){
  15.                 if(i==0 || j==0){
  16.                     L[i][j] = 1;
  17.                 }
  18.                 else{
  19.                     L[i][j] = L[i-1][j-1] + 1;
  20.                 }
  21.                 if(L[i][j] > longest){
  22.                     longest = L[i][j];
  23.                 }
  24.             }
  25.             else{
  26.                 L[i][j] = 0;
  27.             }
  28.         }
  29.     }
  30.    
  31.     var shortestLength = start.length + target.length - 2*longest;
  32.     var bruteForceLength = start.length + target.length;
  33.  
  34.     var probabilities = [0,0,1/36,2/36,3/36,4/36,5/36,6/36,5/36,4/36,3/36,2/36,1/36];
  35.     var possibles = [false,false];
  36.    
  37.     for(var i=2; i<=12; i++){
  38.         if(i>=bruteForceLength || (i>=shortestLength && (i-shortestLength)%2==0)){
  39.             possibles.push(true);
  40.         }
  41.         else{
  42.             possibles.push(false);
  43.         }
  44.     }
  45.     var sum = 0;
  46.     for(var i=2; i<=12; i++){
  47.         if(possibles[i]){
  48.             sum+=probabilities[i];
  49.         }
  50.     }
  51.     console.log(sum.toFixed(4));
  52.    
  53. }
  54.  
  55. process.stdin.resume();
  56. process.stdin.setEncoding("ascii");
  57. _input = "";
  58. process.stdin.on("data", function (input) {
  59.     _input += input;
  60. });
  61.  
  62. process.stdin.on("end", function () {
  63.    processData(_input);
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement