Блог пользователя pritishn

Автор pritishn, 3 года назад, По-английски

You are given a binary string (consisting of 0,1) and you can rotate the string in any order. Basically if the string is s1s2s3...s(n-1)s(n) , you can make it s2s3s4,,,s(n-1)s(n)s1 .

Print out the rotation which has the maximum value in binary representation.

For example:
s=0110
ans= 1100

|s| <= 10^5

Can anyone suggest any approach ? I'm unable to think of any fast approach.

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

It's similiar to this one i guess.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Oh..thanks. But it had no explanation for it's editorial.

    • »
      »
      »
      3 года назад, # ^ |
      Rev. 4   Проголосовать: нравится +17 Проголосовать: не нравится

      Make a new string $$$x = s + s$$$

      Replace all '1' by 'z' and all '0' by 'a'.

      Find largest lexicographic sub-string of size $$$n$$$ in string $$$x$$$ using the concept mentioned here : https://codeforces.com/blog/entry/75953

      When you finally find the required substring , print it till length-'n' only

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      There is an exact question on Hackerearth same as yours and has linear solution but i can't find it now. But we can solve it using binary search to find lexographically largest prefix(using rolling hash).

»
3 года назад, # |
  Проголосовать: нравится +22 Проголосовать: не нравится

This may be overkill, but it shouldn't be hard with a suffix tree.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится

You can use this:Lexicographically minimal string rotation

Take the complement of the string run the algorithm and then take the complement again.

»
3 года назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

We can maintain the binary representations of each prefix and suffix of the string. Then for each position, calculate what will be the result in binary if that position was the first one.

Eg — s = 0110

pref vals = 0 1 3 6

suff vals = 6 6 2 0

then for the indices we have 0th — 6

1st — 12 = (6x2 + 0)

2nd — 9 = (2*4 + 1)

3rd — 3 = (0*8 + 3)

Hence answer is 12 (1100). Complexity should be O(n)

»
3 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

Replace 0 with a and 1 with b and then duplicate the string and find suffix array, answer is last value in the array.

»
3 года назад, # |
  Проголосовать: нравится +28 Проголосовать: не нравится

to compare to 2 possible choices.binary search for the first i such that first (i-1) characters of both choices are same and ith character is different.this can be done with hashing.you need to do this n-1 times so O(nlogn)

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

What you are looking for is lexicographically maximum cyclic shift of binary string. Link This should help.