算法的一组规则的匹配卡 [英] Algorithm for matching cards to a set of rules

查看:156
本文介绍了算法的一组规则的匹配卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个特殊的问题,我似乎没有能够围绕缠上了我的头上。我会马上进去。

I've run into a peculiar problem which I don't seem to be able to wrap my head around. I'll get right into it.

问题是匹配的一组卡的一组规则。

The problem is matching a set of cards to a set of rules.

有可能定义一组规则为字符串。它是由对&LT逗号分隔的元组;套装>:<价值> 。例如 H:4,S:1 应与四心和黑桃的。它也可以通配符,例如 *:* 匹配任何卡, D:* 与匹配任何卡钻石套装,以及 *:2 匹配两人在任何诉讼。规则可以用逗号组合: *:*,*:*,H:4 将匹配一组卡,如果持有2随机卡和四心的

It is possible to define a set of rules as a string. It is composed of comma separated tuples of <suit>:<value>. For example H:4, S:1 should match Four of Hearts and Ace of Spades. It is also possible to wildcard, for example *:* matches any card, D:* matches any card with in the diamond suit, and *:2matches a Two in any suit. Rules can be combined with comma: *:*,*:*,H:4 would match a set of cards if it held 2 random cards and a Four of Hearts.

到目前为止好。解析器这是很简单的直线前进写。这里谈到棘手的部分。

So far so good. A parser for this is easy and straight forward to write. Here comes the tricky part.

为了方便撰写这些规则,两个结构,可用于西装和价值。这是&LT; (合法的西装和值), + N (仅适用于价值的法律),其中n是一个数。 &LT; 的意思是一样的previous匹配和 + N 表示N比$ P $更高pvious比赛。举个例子:

To make it easy to compose these rules, two more constructions can be used for suit and value. These are < (legal for suit and value) and +n (legal only for value) where n is a number. < means "the same as previous match" and +n means "n higher than previous match". An example:

*:*, <:*, *:<

表示:匹配任何一张牌,然后搭配的同花色的第一场比赛的卡片,旁边有相同的值的第二场比赛匹配另一张卡。这只手将匹配:

Means: match any card, then match a card with the same suit as the first match, next match another card with the same value as the second match. This hand would match:

H:4,H:8,C:8

由于四心和八心是一样的西装,而八心和俱乐部的八是相同的值。

Because Hearts of Four and Hearts of Eight is the same suit, while Eight of Hearts and Eight of Clubs is the same value.

这是允许有更多的牌,只要所有规则的匹配(因此,加入 C:10 上面的手仍然符合该规则)。

It is allowed to have more cards as long as all rules match (so, adding C:10 to the above hand would still match the rule).

在解决这个基本上走的是一套应匹配的卡,试图将第一条规则适用于它我的第一种方法。如果是匹配的,我转移到下一个规则,并试图从套牌与之相匹配的,等等,直到所有规则进行匹配,或者我发现一个规律不匹配。这种方法有(至少)一个缺陷,考虑上面的例子上面: *:*,&LT; *,*:&LT; ,但在这个顺序卡: H:8,C:8,H:4

My first approach at solving this is basically taking the set of cards which should be matched, attempting to apply the first rule to it. If it matched, I moved on to the next rule and attempted to match it from the set of cards, and so on until either all rules were matched, or I found a rule that didn't match. This approach have (at least) one flaw, consider example above above: *:*,<:*,*:<, but with the cards in this order: H:8,C:8,H:4.

  • 这将匹配H:8的第一个规则。 匹配:H:8
  • 接下来,它试图找到一个具有相同的花色(红桃)。有一个四心的。 匹配:H:8,H:4
  • 在移动的,它要找到具有相同的值(四)卡和失败。
  • It would match the H:8 of for the first rule. Matched: H:8
  • Next it attempts to find one with the same suit (Hearts). There is a Four of Hearts. Matched: H:8, H:4
  • Moving on, it want to find a card with the same value (Four), and fails.

我不希望的一套卡片被命令对结果有任何影响,因为它在上面的例子中的方式。我可以排序的套牌,如果我能想到的任何一套规则运作良好,任何伟大的战略。

I don't want the way the set of cards is ordered to have any impact on the result as it does in the above example. I could sort the set of cards if I could think of any great strategy that worked well with any set of rules.

我不认识的卡的数量或数量OOF规则,所以蛮力的做法是行不通的。

I have no knowledge of the quantity of cards or number oof rules, so a brute force approach is not feasible.

感谢您对远读这篇文章,我很感激任何提示或洞察力。

Thank you for reading this far, I am grateful for any tip or insight.

推荐答案

您的问题实际上是一个排序问题。这里有一个简单的版本吧:

Your problem is actually an ordering problem. Here's a simple version for it:

给出的数字和图形的输入顺序,重新排列,使它们适合的模式。该模式可以包含*,意思是任意数量和>,意为大于previous数量。

given an input sequence of numbers and a pattern, reorder them so that they fit the pattern. The pattern can contain "*", meaning "any number" and ">", meaning "bigger than the previous number.

例如,给定的模式[* *>>]与序列[10 10 2 1]这样的顺序存在,并且它是〔10 1 2 10]。一些投入可能给没有输出,其他1,而即使是其他许多(想输入[10 10 21]和模式[* * * *])。

For example, given pattern [* * > >] and sequence [10 10 2 1] such an ordering exists and it is [10 1 2 10]. Some inputs might give no outputs, others 1, while even others many (think the input [10 10 2 1] and the pattern [* * * *]).

我想说的是,一旦你做这个简单问题的解决方案,切换到你的问题是增加一个维度,一些运营商的只是一个问题。对不起,不是更多的帮助:/

I'd say that once you have the solution for this simplified problem, switching to your problem is just a matter of adding another dimension and some operators. Sorry for not being of more help :/ .

LE。请记住,如果允许的字符符号是有限的(即4),也是允许的数量(即9)事情可能会变得更加容易。

LE. keep in mind that if the allowed character symbols are finite (i.e. 4) and also the allowed numbers (i.e. 9) things might get easier.

这篇关于算法的一组规则的匹配卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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