I have 2 strings for example:
abcabc and bcabca
or
aaaabb and abaaba
I checked that second string is the same or not like first string but shifted.
bcabca = ..abca + bc...
Here is code it works but too slow:
using System;
public class TOPSES
{
static string t1, t2;
static char[] t1c;
static char[] t2c;
static void Main()
{
int t;
t = int.Parse(Console.ReadLine());
while (t > 0)
{
t1 = Console.ReadLine();
t2 = Console.ReadLine();
string result = "no";
t1c = t1.ToCharArray();
t2c = t2.ToCharArray();
result = ChangeString(t1c[0], 0);
Console.WriteLine(result);
t--;
}
}
public static string ChangeString(char letter, int startIndex)
{
int index = t2.IndexOf(letter, startIndex);
if (index == -1)
return "no";
else
{
string newString = t2.Remove(0, index);
newString += t2.Remove(index, t2.Length - index);
if (t1 != newString)
return ChangeString(letter, ++index);
return "yes";
}
}
}
Question:
How to improve this code to make program faster?
Also it will be nice, if someone could explain what in this code make this 'program' slow.
Thanks Leonid for source!
Code look now that:
using System;
using System.Linq;
public class TOPSES
{
static void Main()
{
string t1, t2;
int t;
t = int.Parse(Console.ReadLine());
while (t > 0)
{
t1 = Console.ReadLine();
t2 = Console.ReadLine();
string result = "no";
if ((t1 + t1).IndexOf(t2) != -1)
result = "yes";
Console.WriteLine(result);
t--;
}
}
}
booltrue/falseinstead of"yes"/"no". You should be able to time the bottleneck yourself. – Leonid Feb 25 at 0:22aais a valid cycling ofaabbcc. – Bobson Feb 25 at 16:06