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.

  1. let i = 0.

  2. let s = null

  3. let c = prefix[i]

  4. remove strings a a if a[i] != c , if a. replace s a if a.length == + 1.

  5. increment i.

  6. 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

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -