Answer the question
In order to leave comments, you need to log in
How to effectively create an "infinite" string?
Hello.
The essence of the problem is that given a string str, the index of the beginning of which must be found in another string, numbers.
The string numbers looks like: "12345678910", that is, numbers in ascending order.
Let's say str = "123", then we need to print 0.
But if str is equal to, for example, "15", then we still have to find the correct index (that is, as I understand it, turn numbers into "123456789101112131415 ..."
Itself I can do the task, but it's too slow. I've been trying to optimize for the second day, but the best that I got is below.
Can anyone tell me something? I haven't been programming for so long, so I could write complete nonsense.
Examples of input and output values:
str = "456", output = 3.
str = "
str="99100", output=187;
str = "123456798", output = 1000000071 (here I can't even get the result, because it takes a very long time to complete).
static long findPosition(string str) {
StringBuilder numbers = new StringBuilder("12345678910");
BigInteger i = 10;
StringBuilder temp = new StringBuilder(1000);
// FindSubstring - алгоритм Кнутта-Морриса-Пратта для поиска индекса
while (FindSubstring(str, numbers.ToString()) == -1) {
// Здесь я часто менял "j < 100" на что-то другое, для разных значений скорость изменяется
for (int j = 0; j < 100; j++) {
// Здесь так же я менял на добавление большего количеств чисел, скорость тоже значительно увеличивалась, но
// но все равно программа работает медленно
temp.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8, i + 9, i + 10);
i += 10;
}
numbers.Append(temp);
temp.Clear();
}
return FindSubstring(str, numbers.ToString());
}
Answer the question
In order to leave comments, you need to log in
The essence of the task is to calculate the index without creating a string numbers
. Fast and without eating memory.
1. First - you need to draw up a formula for increasing the bit depth of positions depending on the input.
2. Then - position recursively starting from the most significant digit - in depth.
3. All jumps - sum up and get the position.
(this is a problem for solving fractal equations)
Of course, finding an index analytically is a true path, but it is also possible to do it "on the forehead". For str = "123456798" you will get the answer in 3 minutes :)
using System;
using System.Collections.Generic;
using System.Linq;
namespace FindString
{
class Program
{
static void Main(string[] args)
{
string str = "123456798";
string buf = string.Concat(Generator.All().Take(str.Length));
ulong index = 0;
foreach (var c in Generator.All().Skip(str.Length))
{
if (buf == str)
{
break;
}
buf = buf.Substring(1) + c;
++index;
}
Console.WriteLine("index is {0}", index);
Console.ReadKey();
}
}
public class Generator
{
public static IEnumerable<char> All()
{
ulong v = 0;
while(true)
{
++v;
foreach(var c in v.ToString().ToCharArray())
{
yield return c;
}
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question