Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Automat (inpstr) {
- this.finstate=inpstr.length;
- let alphabet = new Array();
- let del = new Array();
- for (let j = 0; j <= inpstr.length; j++) {
- del[j] = new Array();
- }
- for (let i = 0; i < inpstr.length; i++) {
- alphabet[inpstr.charAt(i)] = 0;
- }
- for (let i in alphabet) {
- del[0][i] = 0;
- }
- for (let j = 0; j < inpstr.length; j++) {
- let prev = del[j][inpstr.charAt(j)];
- del[j][inpstr.charAt(j)] = j + 1;
- for(let i in alphabet) {
- del[j + 1][i] = del[prev][i];
- }
- }
- this.state = 0;
- this.change = function (symbol) {
- if (del[this.state][symbol] == null) {
- this.state = 0;
- } else {
- this.state = del[this.state][symbol];
- return this.finstate == this.state;
- }
- }
- }
- function Find(text, target) {
- let result = [];
- var automat = new Automat(target);
- for (let i = 0; i < text.length; i++) {
- if (automat.change(text.charAt(i))) {
- result.push(i - target.length + 1);
- }
- }
- if (result.length == 0) console.log('Not found!');
- return result;
- }
- test();
- function test() {
- const fs = require('fs');
- let text = fs.readFileSync("message.txt", "utf8");
- let target = "ef"
- let indexes = Find(text, target);
- console.log(indexes);
- }
Add Comment
Please, Sign In to add comment