7Bpencil

Automat

Dec 22nd, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Automat (inpstr) {
  2.     this.finstate=inpstr.length;
  3.     let alphabet = new Array();
  4.     let del = new Array();
  5.    
  6.     for (let j = 0; j <= inpstr.length; j++) {
  7.         del[j] = new Array();
  8.     }
  9.     for (let i = 0; i < inpstr.length; i++) {
  10.         alphabet[inpstr.charAt(i)] = 0;
  11.     }
  12.     for (let i in alphabet) {
  13.         del[0][i] = 0;
  14.     }
  15.     for (let j = 0; j < inpstr.length; j++) {
  16.         let prev = del[j][inpstr.charAt(j)];
  17.         del[j][inpstr.charAt(j)] = j + 1;
  18.         for(let i in alphabet) {
  19.             del[j + 1][i] = del[prev][i];
  20.         }
  21.     }
  22.    
  23.     this.state = 0;
  24.     this.change = function (symbol) {
  25.         if (del[this.state][symbol] == null) {
  26.             this.state = 0;
  27.         } else {
  28.             this.state = del[this.state][symbol];
  29.             return this.finstate == this.state;
  30.         }
  31.     }
  32. }
  33.  
  34.  
  35. function Find(text, target) {
  36.     let result = [];
  37.     var automat = new Automat(target);
  38.    
  39.     for (let i = 0; i < text.length; i++) {
  40.         if (automat.change(text.charAt(i))) {
  41.             result.push(i - target.length + 1);
  42.         }
  43.     }
  44.  
  45.     if (result.length == 0) console.log('Not found!');
  46.     return result;
  47. }
  48.  
  49.  
  50. test();
  51. function test() {
  52.     const fs = require('fs');
  53.     let text = fs.readFileSync("message.txt", "utf8");
  54.     let target = "ef"
  55.     let indexes = Find(text, target);
  56.     console.log(indexes);
  57. }
Add Comment
Please, Sign In to add comment