A
A
a2NaF2019-02-22 19:52:00
C++ / C#
a2NaF, 2019-02-22 19:52:00

How to quickly select a substring from a string?

I have a string, at each iteration, I need to remove the first element and add a new one to the end. Is it possible to do such an update faster than the length of the string? And a more general case: how to select a substring from a string from a certain index to another index (is there such a function in std)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
a2NaF, 2019-02-22
@a2NaF

In the comments , Roman suggested that there is substr from std

M
Mercury13, 2019-02-22
@Mercury13

Use ring buffer. You will have to pee a little, but what is it for and what will be the complexity of the rest of the work?

T
the1vanyes, 2019-02-23
@the1vanyes

You can do this operation not in O(n), but in O(log n), using a Cartesian tree by an implicit key. It must be built on the characters of the given string. Construction is O(n*log(n)), but deleting and inserting from an arbitrary place (including deleting a character from the beginning and inserting it to the end) takes O(log(n).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question