S
S
Sergey2019-09-02 02:17:41
Algorithms
Sergey, 2019-09-02 02:17:41

How to sort without strings when sorting an array of strings?

There is an array of strings
. You need to sort it and take the last character from each string.
These strings are generated according to the algorithm: the last character is moved to the first place.
I thought that it might be possible not to store them somehow, but simply to take the last character from the generated string and the code by which it would then be possible to sort the last characters that I took.
line code and its last character

public class Element
    {
        public char Symbol;
        public int Hash;

        public static Element Create(StringBuilder str)
        {
            int code = 0;

            //как нибудь считается этот код)
            for (int i = 0; i < str.Length; i++)
            {
                code +=(str[i] - 'a'+ 1) * (str.Length - i) + 127 *(str.Length - i);
            }


            return new Element { Symbol = str[str.Length - 1], Hash = code};
        }
    }

permutation in the loop and calling Element
for (int i = 0; i < strL; i++)
            {
                curStr.Insert(0, curStr[strL - 1]);
                curStr.Remove(strL, 1);
                list.Add(Element.Create(curStr));
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2019-09-05
@Star_Lord

That's what I did

private class VirtualStringBWT : IComparable<VirtualStringBWT>, IEnumerable<char>
        {
            char[] _sourse;
            int _shiftRight;

            public VirtualStringBWT(char[] sourse, int shift)
            {
                if (shift >= sourse.Length)
                    throw new ArgumentOutOfRangeException("Нельзя сдвигать на число большее или равное количеству символов!");

                _sourse = sourse;
                _shiftRight = shift;
            }

            public int CompareTo(VirtualStringBWT other)
            {
                if (other == null)
                    throw new NullReferenceException();

                IEnumerator<char> thisEnumerator = this.GetEnumerator();
                IEnumerator<char> otherEnumerator = other.GetEnumerator();

                //строки одинаковой длины
                while (thisEnumerator.MoveNext() && otherEnumerator.MoveNext())
                {
                    if (thisEnumerator.Current > otherEnumerator.Current)
                        return 1;
                    else if (thisEnumerator.Current < otherEnumerator.Current)
                        return -1;
                }

                return 0;
            }

            public IEnumerator<char> GetEnumerator()
            {
                int startI = _sourse.Length - _shiftRight;
                int i = startI;
                while (i < _sourse.Length)
                {
                    yield return _sourse[i];
                    i++;
                }

                i = 0;

                while (i < startI)
                {
                    yield return _sourse[i];
                    i++;
                }
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
    }

1) From the string to be encoded, I make an array of characters, which will be the same for all VirtualString.
2) Then I create n pieces of VirtualString where I pass an array of characters and a shift.
for (int i = 0; i < _source.Length; i++)
       list.Add(new VirtualStringBWT(t, i));

3) When I refer to the VirtualString object, it starts to produce an array from a certain position.
And I don't create 200 rows.
Hooray)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question