Skip to content

Instantly share code, notes, and snippets.

@paulll
Created September 12, 2015 13:29
Show Gist options
  • Save paulll/6007caf542147533932d to your computer and use it in GitHub Desktop.
Save paulll/6007caf542147533932d to your computer and use it in GitHub Desktop.
Проверка, входит ли строка в прямое произведения множества других строк
var set1 = 'Привет, Здраствуй, Здраствуйте, Приветик, Превед, Здаров, Прив, Хелло'.split(', '),
set2 = 'Что, Чего, Што, Чо'.split(', '),
set3 = 'Делаешь, Творишь, Думаешь, Пишешь, Делаете, Пишете, Творите, Думаете'.split(', '),
set = [set1, set2, set3],
str = 'ЗдраствуйтеЧоДелаете';
/**
* Все возможные комбинации можно представить деревом (графом),
* можно найти единственно верный путь, просто отсекая явно неподходящие ветки.
* Получается что-то вроде поиска в глубину с ограничением.
*/
function test(string, stack, tokens) {
return stack[0].find(function(value) {
if (string.indexOf(value) === 0) {
if (string.length - value.length === 0) {
tokens.unshift(value);
return true;
} else {
if (test(string.substr(value.length), stack.slice(1), tokens)) {
tokens.unshift(value);
return true;
} else {
return false;
}
}
} else {
return false;
}
});
};
var tokens = [];
test(str, set, tokens);
console.log(tokens);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment