定subsrings和一个字符串的列表,打印出来的项目列表中,如果它是一个串它是、字符串、打印出来、项目

2023-09-11 02:47:39 作者:我不是空气

鉴于subsrings的列表和一个字符串,打印出来的项目列表中,如果它是一个串。

Given a list of subsrings and a string, print out the item in the list if it is a substring.

什么是最有效的方式做到这一点,而无需使用任何字符串的方法?

What's the most efficient way to do this without using any substring methods?

示例:

输入: [猫,狗,富,foopoo,foopo,没了,dogf]

输出:catdogfoopoo

说明:

c是在地图索引0。

通过猫的长度环路和比较,如果其与从0至猫

Loop through the length of "cat" and compare if its the same as from 0 to the length of "cat"

如果有,打印。

我的想法:

所以我的想法,到目前为止是,你将通过字符串循环,并给出每个字符映射到指数的ArrayList

So my idea so far is that you would loop through the string given and map each character to an arraylist of indices

≤(C,[0]),(一,[1]),(R,[2]),(D,[3]),(邻,[4,7,8,10,11 ]),(克,[5]),(F,[6]),(对,[4])>

<(c, [0]), (a, [1]), (r, [2]), (d, [3]), (o, [4,7,8,10,11]), (g, [5]), (f, [6]), (p, [4])>

然后通过串的列表循环。

and then you loop through the list of substrings.

for (int x = 0; x < list.length; x++) {
    String s = list.get(x);
    if (s.get(s.charAt(0)) != null)
         //loop through, comparing from whether the word is in the string

但这并不需要的事实,即如果富已经被认为是一个字符串,它应该很容易检查foopoo是子(不通过富循环的话)

but this doesn't take advantage of the fact that if "foo" was already found to be a substring, it should be easy to check if "foopoo" is a substring (without looping through foo already)

我有点困在那里,因为我敢肯定有一个更有效的方法来做到这一点。不使用包含或诸如此类的东西(未更有效)

I'm a bit stuck there because I'm sure there's a more efficient way to do this. Without using "contains" or whatnot (which is NOT more efficient)

推荐答案

也许你应该在子排序,并产生类似于这样的数据结构(这是链表的数组):

Maybe you should sort the substrings and generate a data structure similar to this (it is an array of linked lists):

[0] cat
[1] dog -> f
[2] foo -> po -> o
[3] nope

在此结构中,你可以找到的子串狗和dogf,以及富,foopo和foopoo。它类似于搜索的字典中。

In this structure you can find the substrings "dog" and "dogf", as well as "foo", "foopo" and "foopoo". It is similar to searching in a dictionary.

您开始在你的catdogfoopoo字符串的索引0,并开始通过字典来搜索,即寻找它与C开头的条目。你发现了结构索引0包含'C',所以你决定步行穿过它的长度。所以,你需要输入的长度(3个字符),看看是否从索引0开始的三个字符等于它(这是一个字符串比较,它不能帮助)。他们,所以你打印的猫,因为你发现,包含在你的字符串整个项目。

You start at index 0 of your "catdogfoopoo" string, and starts to search through the dictionary, i.e. look for an entry which starts with 'c'. You find out that index 0 of the structure contains 'c', so you decide to walk through its length. So you take the entry length (3 characters) and see if the three characters starting at index 0 are equal to it (it's a string comparison, it can't be helped). They are, so you print "cat" because you found a whole entry that is contained in your string.

未来指数是3,即字符'D',和你在的位置[0]的词典的第一个条目。有在这个位置没有进一步的条目,所以你必须寻找一个不同的入口开始,D让你找到狗的位置[1]中的第一项。你比较这条目(这是做出来的三个字符)的字符串中的字符从索引4,你会发现他们是平等的,所以您打印的狗。接下来指数为6,你比较它的下一项是F,你会发现他们是平等的,所以你打印出dogf。

Next index is 3 i.e. character 'd', and you are at the first entry of position [0] of the dictionary. There are no further entries in this position so you must look for a different entry starting with 'd' so you find "dog" as the first entry of position [1]. You compare this entry (which is made out of three characters) with the characters in the string starting at index 4 and you find they are equal, so you print "dog". Next index is 6 and you compare it the next entry which is "f" and you find they are equal so you print "dogf".

,以此类推,直到你打印出富,foopo和foopoo。

And so on until you print "foo", "foopo" and "foopoo".

希望这是显而易见的。如果你觉得有用,我可以提供我是如何创建的结构细节。

Hope it is clear. If you find it useful I can provide the details on how I created the structure.