Is there any better algorithm?

Revision en1, by a_gt3_rs, 2024-10-16 14:44:09

Given the problem below: Find the lexicographically minimum cyclic shift of a binary string s of length n ($$$n\leq 3*10^4$$$). A normal brute force solution runs in only 0.05s:

    string x = s;
    for (int i = 1; i < s.size(); i++)
    {
        string x2 = string(s.begin() + i, s.end()) + string(s.begin(), s.begin() + i);
        if (x2 < x)
            x = x2;
    }
    cout << x2 << endl;

I only know an $$$O(n^2)$$$ algorithm (the algorithm above) or $$$O(n^2/w)$$$ using bitsets. Is this the intended solution, or is there any better algorithm?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English a_gt3_rs 2024-10-16 14:51:37 0 (published)
en3 English a_gt3_rs 2024-10-16 14:51:20 0 (saved to drafts)
en2 English a_gt3_rs 2024-10-16 14:44:59 1 Tiny change: ' cout << x2 << endl;\' -> ' cout << x << endl;\'
en1 English a_gt3_rs 2024-10-16 14:44:09 598 Initial revision (published)