Maximal suffix

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
nibbler
New poster
Posts: 22
Joined: Fri Jun 04, 2004 10:30 pm

Maximal suffix

Post by nibbler »

Problem: given a text determine lexicographically maxiaml suffix of a string in O(n).

For example:
cacbca has the max suffix cbca.

I read that this can be done with technique similar to one used in KMP, using the maximal suffix and at the same time a prefix of a word, but I don't know how to implement it.

Any help is appreciated
Minilek
Learning poster
Posts: 90
Joined: Tue Jul 27, 2004 9:34 am
Location: Cambridge, MA
Contact:

Post by Minilek »

build a suffix tree then start at the root and always go down to the lexicographically highest child until you hit a leaf. this will end you with the suffix you are looking for.

One way to build a suffix tree is Ukkonen's Algorithm. This site explains
how that works and even has a sample implementation in C++ at the
very end.

http://www.dogma.net/markn/articles/suffixt/suffixt.htm
Cosmin.ro
Learning poster
Posts: 95
Joined: Thu Aug 21, 2003 12:02 am

Post by Cosmin.ro »

There are simpler algorithms wich are specialised for this task. Like the next one (it gets thwe minimal suffix not the maximal one):

Code: Select all

for (i = 0; i<N;i++) C[N+i]=C[i];
   k=0;n=1;l=0;
   while (n<N&&k+l+1<N) {
	   if (C[k+l]==C[n+l]) l++;
	   else if (C[k+l] < C[n+l]) {
             n=n+l+1;l=0;
	   } else {
		   h=n>k+l+1?n:k+l+1;
		   k=h;n=1+h;l=0;
	   }
   }
Post Reply

Return to “Algorithms”