c - Find longest suffix of string in given array -
given string , array of strings find longest suffix of string in array.
for example
string = google.com.tr
array = tr, nic.tr, gov.nic.tr, org.tr, com.tr
returns com.tr
i have tried use binary search specific comparator, failed.
c-code welcome.
edit:
i should have said im looking solution can work can in preparation step (when have array of suffixes, , can sort in every way possible, build data-structure around etc..), , given string find suffix in array fast possible. know can build trie out of array, , give me best performance possible, im lazy , keeping trie in raw c in huge peace of tangled enterprise code no fun @ all. binsearch-like approach welcome.
assuming constant time addressing of characters within strings problem isomorphic finding largest prefix.
let
i = 0
.let
s = null
let
c = prefix[i]
remove strings
a
a
ifa[i] != c
, ifa
. replaces
a
ifa.length == + 1
.increment
i
.go step 3.
is you're looking for?
example:
prefix = rt.moc.elgoog
array = rt.moc, rt.org, rt.cin.vof, rt.cin, rt
pass 0: prefix[0]
'r'
, array[j][0] == 'r'
j
nothing removed array. i + 1 -> 0 + 1 -> 1
our target length, none of strings have length of 1, s
remains null
.
pass 1: prefix[1]
't'
, array[j][1] == 'r'
j
nothing removed array. there string has length 2, s
becomes rt
.
pass 2: prefix[2]
'.'
, array[j][2] == '.'
remaining strings nothing changes.
pass 3: prefix[3]
'm'
, array[j][3] != 'm'
rt.org
, rt.cin.vof
, , rt.cin
strings removed.
etc.
Comments
Post a Comment