(piszę to w kontekście JavaScriptu, ale akceptuje się algorytmicznie poprawną odpowiedź w dowolnym języku)Znajdź najmniejszą unikalny podciąg dla każdej struny w tablicy
Jak odnaleźć najkrótszy podłańcuch każdego elementu w tablicy ciągów, gdzie podciąg NIE jest zawarty w żadnym z pozostałych elementów, ignorując przypadek?
Załóżmy, że mam tablicę wejściowych, takich jak:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
Wyjście powinno być coś takiego:
var uniqueNames = ["ne", "h", "ua", "ka", "i", "r"];
Dla moich celów, można bezpiecznie założyć, że żaden element nie zostanie całkowicie zawarty w kolejny element.
myśli moje
Wydaje się, że ktoś mógłby prawdopodobnie brutalnej siły tej, wzdłuż linii:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], nameInd, windowSize, substrInd, substr, otherNameInd, foundMatch;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each starting index of a substring
for (substrInd = 0; substrInd <= name.length-windowSize; substrInd++)
{
substr = name.substring(substrInd,substrInd+windowSize).toLowerCase();
foundMatch = false;
// For each other name
for (otherNameInd = 0; otherNameInd < names.length; otherNameInd++)
{
if (nameInd != otherNameInd && names[otherNameInd].toLowerCase().indexOf(substr) > -1)
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
// This substr works!
uniqueNames[nameInd] = substr;
break windowLoop;
}
}
}
}
Ale muszę sobie wyobrazić, istnieje bardziej eleganckie rozwiązanie za pomocą prób/drzew przedrostek, tablice sufiksu lub coś tak interesującego.
Edit: Wierzę, że to jest forma wybrana odpowiedź zajęłoby programowo w JavaScript:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], permutations = {}, permutation, nameInd, windowSize, substrInd, substr;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each starting index of a substring
for (substrInd = 0; substrInd <= name.length-windowSize; substrInd++)
{
substr = name.substring(substrInd,substrInd+windowSize).toLowerCase();
permutations[substr] = (typeof permutations[substr] === "undefined")?nameInd:-1;
}
}
}
for (substr in permutations)
{
permutation = permutations[substr];
if (permutation !== -1 && ((typeof uniqueNames[permutation] === "string" && substr.length < uniqueNames[permutation].length) || typeof uniqueNames[permutation] === "undefined"))
{
uniqueNames[permutation] = substr;
}
}
Czy dane wyjściowe próbki są nieprawidłowe? Nie widzę tam 's' i' y', natomiast widzę 'i, h' i' r' ... – Icarus
@Icarus Ah, dobry punkt. 's' i' y' nie są obecne tylko dlatego, że nie szukam najmniejszych podciągów, które pasują do kryteriów, a każdy z nich jest wystarczająco dobry. Przyjmuję odpowiedź, która odwzajemnia ich dwuwymiarowy układ, ale tak naprawdę nie potrzebuję tego poziomu szczegółowości. Równie ważną wersją może być 'var uniqueNames = [" ne "," y "," ua "," ka "," i "," s "];' – Patrick
Czy możliwe jest ograniczenie wprowadzonego alfabetu do 26 znaków (lub coś w tym stylu, po prostu ogranicz to)? –