Size: a a a

2020 June 20

DE

Denis Efremov in pro.js
Что? Ничо не понял
источник

DE

Denis Efremov in pro.js
Зачем?
источник

L

Lupusregina[beta] in pro.js
Denis Efremov
Зачем?
'abced'
'abqw'

 a
  b
 c q
e   w
d
источник

DE

Denis Efremov in pro.js
источник

DE

Denis Efremov in pro.js
источник

L

Lupusregina[beta] in pro.js
ну быстрее не умет он(
источник

DE

Denis Efremov in pro.js
Да понимаю, просто прекол )))
источник

L

Lupusregina[beta] in pro.js
Lupusregina[beta]
'abced'
'abqw'

 a
  b
 c q
e   w
d
точнее это префиксное дерево, выходит
источник

DE

Denis Efremov in pro.js
Да, только нам нужно хранить только каунты для выполнения задачи
источник

DE

Denis Efremov in pro.js
Погоди, {a: {a: true, c: true}}
aa
ac
источник

DE

Denis Efremov in pro.js
А если её создавать сверху
источник

DE

Denis Efremov in pro.js
То есть не строки преобразовывать в неё
источник

DE

Denis Efremov in pro.js
А прописать структуру, объект
источник

DE

Denis Efremov in pro.js
Вложенность 100 это дохуя?
источник

DE

Denis Efremov in pro.js
Как посчитать сколько добавлять букв в объекты?
источник

DE

Denis Efremov in pro.js
Для 10кк true на сотом уровне
источник

L

Lupusregina[beta] in pro.js
Denis Efremov
Погоди, {a: {a: true, c: true}}
aa
ac
class Node {
 constructor(char, parent, inc = true) {
   this.parent = parent
   this.char = char
   this.children = []
   this.leaf = false
   this.numLeafs = 1

   if ( inc )
     while(parent) {
       parent.numLeafs++
       parent = parent.parent
     }
 }
}
class Tree {
 constructor() {
   this.root = new Node("", null, false)
   this.root.numLeafs = 0
 }
 
 add(word, node = this.root) {
   for(let i = 0; i < word.length; i++) {
     const char = word[i]
     const childNode = node.children.find(n => n.char[0] === char)
     if ( childNode ) {
       if ( childNode.char.length === 1 ) {
         node = childNode
         continue
       } else {
         const word1 = childNode.char.slice(1)
         childNode.char = childNode.char[0]
         childNode.children.push( new Node(word1, childNode, false) )
         this.add(word.slice(i + 1), childNode)
         break
       }
     } else {
       node.children.push( new Node(word.slice(i), node, true) )
       break
     }
   }
 }
 getAllNumNodes(children = this.root.children) {
   return children.reduce((s, node) => s + 1 + this.getAllNumNodes(node.children), 0)
 }
 likeStringCount(pattern) {
   let node = this.root

   next:
   while(1) {
     if ( !pattern.length )
       return node.numLeafs

     let children = node.children
     for(const childNode of children) {
       if ( childNode.children.length ) {
         if ( childNode.char === pattern[0] ) {
           pattern = pattern.slice(1)
           node = childNode
           continue next;
         }
       } else {
         if ( childNode.char.indexOf(pattern) === 0 ) {
           pattern = ""
           node = childNode
           continue next;
         }
       }
     }
     
     return 0
   }
 }
}
const t = new Tree

function getStringRand(len = 100) {
 let s = ""
 while(len--)
   s += String.fromCharCode(97 + (Math.random()*25|0))
 return s
}

console.time('init')
for(let i = 0; i < 1e6; i++) {
 const s = getStringRand()
 t.add( s )
}
console.timeEnd('init')

function like(pattern) {
 let tn = Date.now()
   const count = t.likeStringCount(pattern)
 tn = Date.now() - tn
 console.log(`(time: ${ tn }msec) Like '${ pattern }.*' count: ` + count)
}
like('a')
like('ab')
like('abc')
like('abce')
источник

S

Syntax Highlight Bot in pro.js
Lupusregina[beta]
class Node {
 constructor(char, parent, inc = true) {
   this.parent = parent
   this.char = char
   this.children = []
   this.leaf = false
   this.numLeafs = 1

   if ( inc )
     while(parent) {
       parent.numLeafs++
       parent = parent.parent
     }
 }
}
class Tree {
 constructor() {
   this.root = new Node("", null, false)
   this.root.numLeafs = 0
 }
 
 add(word, node = this.root) {
   for(let i = 0; i < word.length; i++) {
     const char = word[i]
     const childNode = node.children.find(n => n.char[0] === char)
     if ( childNode ) {
       if ( childNode.char.length === 1 ) {
         node = childNode
         continue
       } else {
         const word1 = childNode.char.slice(1)
         childNode.char = childNode.char[0]
         childNode.children.push( new Node(word1, childNode, false) )
         this.add(word.slice(i + 1), childNode)
         break
       }
     } else {
       node.children.push( new Node(word.slice(i), node, true) )
       break
     }
   }
 }
 getAllNumNodes(children = this.root.children) {
   return children.reduce((s, node) => s + 1 + this.getAllNumNodes(node.children), 0)
 }
 likeStringCount(pattern) {
   let node = this.root

   next:
   while(1) {
     if ( !pattern.length )
       return node.numLeafs

     let children = node.children
     for(const childNode of children) {
       if ( childNode.children.length ) {
         if ( childNode.char === pattern[0] ) {
           pattern = pattern.slice(1)
           node = childNode
           continue next;
         }
       } else {
         if ( childNode.char.indexOf(pattern) === 0 ) {
           pattern = ""
           node = childNode
           continue next;
         }
       }
     }
     
     return 0
   }
 }
}
const t = new Tree

function getStringRand(len = 100) {
 let s = ""
 while(len--)
   s += String.fromCharCode(97 + (Math.random()*25|0))
 return s
}

console.time('init')
for(let i = 0; i < 1e6; i++) {
 const s = getStringRand()
 t.add( s )
}
console.timeEnd('init')

function like(pattern) {
 let tn = Date.now()
   const count = t.likeStringCount(pattern)
 tn = Date.now() - tn
 console.log(`(time: ${ tn }msec) Like '${ pattern }.*' count: ` + count)
}
like('a')
like('ab')
like('abc')
like('abce')
источник

L

Lupusregina[beta] in pro.js
вот какая идея
источник

DE

Denis Efremov in pro.js
2**26 это уже 67КК
источник