Hello,
Please I have a sequence of words S, and a sequence of words T. How do I find the shortest continuous subsequence of words in S such that the words in T appear in the order (in c#)
An example is
S ==> One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them
T ==> find, them, all
Result ==> find them, One Ring to bring them all
My current source code is:
public HashSet<string> details()
{
String a = "One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them";
String b = "find, them, all";
string[] avalues = a.Split(',');
string[] bvalues = b.Split(',');
HashSet<string> set = new HashSet<string>();
int index = -1;
bool found = false;
string text = "";
for (int q = 0;q < bvalues.Length; q++)
{
if (q == 0)
{
text = avalues[q];
set.Add(text);
}
else if (q > 0)
{
text = avalues[q].Substring(0, avalues[q].LastIndexOf(bvalues[q])) + bvalues[q];
set.Add(text);
}
}
char[] split = new char[] { ' ', '\t', '\r', '\n', '.', ';', ',', ':', };
for (int r = 0; r <= set.Count-1; r++)
{
string setelem = set.ElementAt(r - 1); ;
{
string[] parts = set.ElementAt(r).Split(split, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
if (set.ElementAt(r).Contains(part))
{
int indexa = set.ElementAt(r).IndexOf(part);
set.ElementAt(r - 1).Remove(indexa);
}
}
}
return set;
}
but its not giving me the desired output.I probably could not get the right approach. Can someone please help a brother in the house? Thanks...