执行单词的后续序列 [英] Implementation of subsequence of words

查看:65
本文介绍了执行单词的后续序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



请我有一系列单词S和一系列单词T.如何找到S中单词的最短连续子序列T中的单词出现在顺序中(在c#中)

一个例子是



S ==>一个环统治它们,一个环找到它们,一个环带来它们全部并且在黑暗中绑定它们

T ==>找到,他们,所有

结果==>找到他们,One Ring带给他们所有



我目前的源代码是:



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"; // list of words to search
         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)

                    //construct the string to delete
                    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...

推荐答案

如果我正确理解了这个任务,你想找到'find'和'all'之间最短的子字符串'where''在字符串中。



一种方法可能是使用IndexOf。

1.找到'find'这个词并将索引保​​存为indexFind 。

2.找到单词'all',从indexFind开始并将此索引保存为indexAll。

3.检查是否可以找到他们之间的单词两个索引。

4.如果没有,请使用indexAll作为起始位置从步骤2开始重复。

5.最后,复制indexFind和indexAll之间的子字符串。 br />


注意:您还需要检查,例如,序列不包含重复项,例如找到它们找到所有。在这种情况下,我假设你只想要字符串的最后一部分。



如果您需要更通用的解决方案,您可以调整解决方案,如下所示:

1.从集合T中找到第一个单词在集合S中并将索引保​​存为indexFirst。

2.从集合S中的集合T中找到最后一个单词,从indexFirst开始并将此索引保存为indexLast。

3循环设置T并检查单词是否按顺序出现并且只出现一次(没有重复)。

4.如果没有,再进行一次搜索。



我希望这可以帮助你进一步完成学业。

这不是一个现成的解决方案,只是前进方向的指针。
If I understand the task correctly, you want to find the shortest sub-string between 'find' and 'all' where 'them' occurs in the string.

One approach could be to use IndexOf.
1. Find the word 'find' and save the index as indexFind.
2. Find the word 'all', starting from indexFind and save this index as indexAll.
3. Check if you can find the word 'them' between the two indices.
4. If not, repeat from step 2 using indexAll as the start position.
5. Finally, copy the sub-string between indexFind and indexAll.

Note: You also need to check that, for example, the sequence does not contain duplicates like 'find them find them all'. In this case I presume you want only the last part of the string.

If you need a more generic solution, you can adapt the solution something like this:
1. Find the first word from set T in set S and save the index as indexFirst.
2. Find the last word from set T in set S, starting from indexFirst and save this index as indexLast.
3. Loop through set T and check if the words appear in sequence and only once (no duplicates).
4. If not, do another search round.

I hope this helps you further with your school work.
It is not a ready solution, just a pointer for the way forward.


这篇关于执行单词的后续序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆