TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
string Namespace Reference

string manipulation algorithms More...

Functions

template<typename T >
size_t duval (const T &s)
 Find the lexicographically smallest cyclic shift of a sequence.
 

Detailed Description

string manipulation algorithms

for std::array for assert for std::size_t for std::deque for std::cout and std::endl for std::string for std::vector

Function Documentation

◆ duval()

template<typename T >
size_t string::duval ( const T & s)

Find the lexicographically smallest cyclic shift of a sequence.

Template Parameters
Ttype of the sequence
Parameters
sthe sequence
Returns
the 0-indexed position of the least cyclic shift of the sequence

Definition at line 49 of file duval.cpp.

49 {
50 size_t n = s.size();
51 size_t i = 0, ans = 0;
52 while (i < n) {
53 ans = i;
54 size_t j = i + 1, k = i;
55 while (j < (n + n) && s[j % n] >= s[k % n]) {
56 if (s[k % n] < s[j % n]) {
57 k = i;
58 } else {
59 k++;
60 }
61 j++;
62 }
63 while (i <= k) {
64 i += j - k;
65 }
66 }
67 return ans;
68 // returns 0-indexed position of the least cyclic shift
69}
double k(double x)
Another test function.