Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
NormalizeRotation.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <string/CyclicString.h>
9
10namespace string {
11
12namespace simplify {
13
20public:
32 template < class SymbolType >
34};
35
36template < class SymbolType >
38 const ext::vector < SymbolType > & data = string.getContent ( );
39 int * f = new int [ 2 * data.size ( ) ];
40 int k = 0;
41 f [ 0 ] = -1;
42 for ( size_t j = 1; j < 2 * data.size ( ); ++j ) {
43 int i = f [ j - k - 1 ];
44 while ( i != -1 && data [ j % data.size ( ) ] != data [ ( k + i + 1 ) % data.size ( ) ] ) {
45 if ( data [ j % data.size ( ) ] < data [ ( k + i + 1 ) % data.size ( ) ] ) k = j - i - 1;
46 i = f [ i ];
47 }
48 if ( i == -1 && data [ j % data.size ( ) ] != data [ k % data.size ( ) ] ) {
49 if ( data [ j % data.size ( ) ] < data [ ( k + i + 1 ) % data.size ( ) ] ) k = j;
50 f [ j - k ] = -1;
51 } else f [ j - k ] = i + 1;
52 }
53 delete [] f;
54
55 if(k == 0)
56 return string;
57
59 for ( unsigned l = k % data.size ( ); l < data.size() + k % data.size ( ); ++l ) {
60 rotated.push_back ( data [ l % data.size ( ) ] );
61 }
62
63 return string::CyclicString < > { string.getAlphabet ( ), rotated };
64}
65
66} /* namespace simplify */
67
68} /* namespace string */
69
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Cyclic string.
Definition: CyclicString.h:60
const ext::set< SymbolType > & getAlphabet() const &
Definition: CyclicString.h:106
Definition: NormalizeRotation.h:19
static string::CyclicString< SymbolType > normalize(const string::CyclicString< SymbolType > &string)
Definition: NormalizeRotation.h:37
int i
Definition: AllEpsilonClosure.h:118
Definition: RandomStringFactory.cpp:12