简体   繁体   English

在有序列表/数组/缓冲区中查找无序值序列

[英]Finding unordered sequence of values in an ordered List/Array/Buffer

I'm trying to create a game in Java. 我正在尝试用Java创建游戏。 I want to create a feature where you can sacrifice items, and if you sacrifice the right 'recipe' of different items, you get something in return. 我想创建一个可以牺牲物品的功能,如果您牺牲了不同物品的正确“配方”,那么您会得到一些回报。 A pseudo example of recipes could be: 配方的伪示例可能是:

recipe1[1, 5, 32]
recipe2[4, 221, 21, 9]

The order of the recipes should not matter - meaning, if I have input = ..., 7, 5, 32, 1 this should return true for recipe1[1, 5, 32] - but the sequence should matter, so input = ..., 5, 7, 32, 1 returns false , even though input does contain all the values of recipe1. 配方的顺序无关紧要-意思是,如果我有input = ..., 7, 5, 32, 1 recipe1[1, 5, 32] ,则recipe1[1, 5, 32]返回应为recipe1[1, 5, 32]顺序应该很重要,因此input = ..., 5, 7, 32, 1 5,7,32,1返回false ,即使输入确实包含配方1的所有值。

I have no real code yet, as I have no idea how to go about this. 我还没有真正的代码,因为我不知道该怎么做。

The code would be much easier to implement if you use a Set of Sets. 如果使用一组集合,则代码将更容易实现。

Set<Set<Integer>> recipes = new HashSet<>();

now you can check if a recipe is part of the set: 现在,您可以检查食谱是否属于套装:

boolean contains = recipes.contains(new HashSet<>(Arrays.asList(1,5,32));

The order is not important, but the exact items are. 顺序并不重要,但确切的项目很重要。


Ah, I think I misunderstood, this is the exact opposite of what you are asking for. 嗯,我想我误会了,这与您要的完全相反。 Dang! ang!

Looking for the items in the right order will perform a lot worse and be more tedious. 按正确的顺序查找商品会导致效果变差,并且更加乏味。 Basically, you can do a Collections.binarySearch() for the first item in the sequence. 基本上,您可以为序列中的第一项执行Collections.binarySearch() And if you find it, keep looking from there. 如果找到它,请继续从那里寻找。 Your data structure would be a List in that case. 在这种情况下,您的数据结构将是一个列表。


No, I think it's closer to my initial understanding after all. 不,我认为这毕竟更接近我最初的理解。 Use the set of sets, but test it for all possible sub-lists of your input. 使用这组集合,但要对输入的所有可能的子列表进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM