Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP程序从两个数组中查找不同的组合_Php - Fatal编程技术网

PHP程序从两个数组中查找不同的组合

PHP程序从两个数组中查找不同的组合,php,Php,皮的父亲丹尼经营着哈克维尔动物园。他要搬到新秀镇去 他想把动物园里所有的动物都带上船。他是 对如何安排它们感到困惑,因为一些物种 不能放在同一个舱内。 有n只动物排成一条直线。每只动物都被识别 按从1到n的唯一数字顺序排列。有m对(a[i],b[i]) 这意味着动物a[i]和b[i]是敌人,不应该是敌人 被关在同一间小屋里。皮善于解决问题,他就这样出现了 通过以下挑战:计算参与的不同组的数量 不要包含任何一对,以免它们成为敌人。定义了一个组 作为一个间隔(x,y),使得x到y范围内的所有动物形成

皮的父亲丹尼经营着哈克维尔动物园。他要搬到新秀镇去 他想把动物园里所有的动物都带上船。他是 对如何安排它们感到困惑,因为一些物种 不能放在同一个舱内。
有n只动物排成一条直线。每只动物都被识别 按从1到n的唯一数字顺序排列。有m对(a[i],b[i]) 这意味着动物a[i]和b[i]是敌人,不应该是敌人 被关在同一间小屋里。皮善于解决问题,他就这样出现了 通过以下挑战:计算参与的不同组的数量 不要包含任何一对,以免它们成为敌人。定义了一个组 作为一个间隔(x,y),使得x到y范围内的所有动物形成一个 小组。确定可根据以下条件形成的组数: 面对Pi的挑战。
例如,假设n=3只动物,m=3对敌人,a=[1,2, 3] b=[3,3,1],动物1是动物3的敌人,动物3是 动物1和2的敌人。因为3是1和2的敌人,所以它 必须在它自己的船舱里。1号和2号动物可以合租,也可以 分开。
有四种可能的分组满足约束条件:
{1,2},{1},{2},{3}

注意,间隔是沿着从1到n连续编号的动物原始线,即在这种情况下为[1,2,3]。 它们不能重新排序。 功能描述 在下面的编辑器中完成angryAnimals函数。功能 必须返回可根据Pi组成的组数 挑战。
angryAnimals具有以下参数:
n:表示独特动物数量的整数
a[a[0],…a[m-1]:整数数组
b[b[0],…b[m-1]:整数数组
约束条件
1.≤ N≤ 10
1.≤ M≤ 10
1.≤ a[i],b[i]≤ n

我已经能够得到不同的组合,但我发现很难比较和删除敌人出现在一起的组合

函数构造($s,$k){ if(is_数组($s)){ $this->s=array\u值($s); $this->n=计数($this->s); }否则{ $this->s=(字符串)$s; $this->n=strlen($this->s); } $this->k=$k; $this->revind(); } 函数键(){ 返回$this->pos; } 函数电流(){ $r=数组(); 对于($i=0;$i<$this->k;$i++) $r[]=$this->s[$this->c[$i]]; 返回为数组($this->s)$r:内爆(“”,$r); } 函数next(){ 如果($this->_next()) $this->pos++; 其他的 $this->pos=-1; } 函数倒带(){ $this->c=范围(0,$this->k); $this->pos=0; } 函数valid(){ 返回$this->pos>=0; } // 受保护函数_next(){ $i=$this->k-1; 而($i>=0&&$this->c[$i]==$this->n-$this->k+$i) $i--; 如果($i<0) 返回false; $this->c[$i]++; 而($i++<$this->k-1) $this->c[$i]=$this->c[$i-1]+1; 返回true; } 自定义测试的示例输入

4
2
1
2
2
3
4
我的输出

1
2
3
4
12
13
14
23
24
34
123
124
134
234
1234
15.

预期产量

1
2
3
4
12
23
34
7.

解释 (1) (1,2)、(2)、(2,3)、(3)、(3,4)、(4)是形成的7组 根据Pi的挑战


我不知道这是否有帮助,但我在这里用回溯编程做了一个C#

驱动程序功能:

public static long angryAnimals(int n, List<int> a, List<int> b)
{
    return count(1, n, new bool[n + 2], a.ToArray(), b.ToArray(), 0, 1, 0);
}
public static int count(int start, int n, bool[] used, int[] a, int[] b, int numgroup, int cur, int c)
{
        if (start <= n || cur != n)
        {
            used[start] = true;
            numgroup++;

            if (numgroup == 1)
            {
                c++;
                return count(start + 1, n, used, a, b, numgroup, start, c);
            }

            for (var j = 0; j < a.Length; j++)
            {
                if (used[a[j]] && used[b[j]])
                {
                    return count(cur + 1, n, new bool[n + 1], a, b, 0, cur, c);
                }
            }

            c++;

            if (start == n)
            {
                return count(cur + 1, n, new bool[n + 1], a, b, 0, cur, c);
            }

            return count(start + 1, n, used, a, b, numgroup, cur, c);
        }
        return c;
    }
公共静态长时间愤怒动物(int n,列表a,列表b)
{
返回计数(1,n,新布尔[n+2],a.ToArray(),b.ToArray(),0,1,0);
}
递归函数:

public static long angryAnimals(int n, List<int> a, List<int> b)
{
    return count(1, n, new bool[n + 2], a.ToArray(), b.ToArray(), 0, 1, 0);
}
public static int count(int start, int n, bool[] used, int[] a, int[] b, int numgroup, int cur, int c)
{
        if (start <= n || cur != n)
        {
            used[start] = true;
            numgroup++;

            if (numgroup == 1)
            {
                c++;
                return count(start + 1, n, used, a, b, numgroup, start, c);
            }

            for (var j = 0; j < a.Length; j++)
            {
                if (used[a[j]] && used[b[j]])
                {
                    return count(cur + 1, n, new bool[n + 1], a, b, 0, cur, c);
                }
            }

            c++;

            if (start == n)
            {
                return count(cur + 1, n, new bool[n + 1], a, b, 0, cur, c);
            }

            return count(start + 1, n, used, a, b, numgroup, cur, c);
        }
        return c;
    }
public static int count(int start、int n、bool[]used、int[]a、int[]b、int numgroup、int cur、int c)
{
如果(开始这里是我的答案#

公共静态长时间愤怒动物(int n,列表a,列表b)
{
int可能性=0;
var ban=新字典();
var min=0;
for(int i=0;i其他)
ban[min]=其他;
}
var=n+1;
对于(int i=n;i>=1;i--)
{
var add=最后一个敌人;
if(ban.TryGetValue(i,out-var))
{
如果(最后一个敌人>敌人)
{
敌人=敌人;
加=敌人;
}
}
可能性+=(加-i);
}
返回可能性;
}

你能给我一个挑战的链接吗?没有。我的导师给了我一个问题的pdf文件,希望我使用文本编辑器。我可以分享这个pdf。你是在要求我们为你解决挑战吗?没有。我就快到了。我只是解释一下如何从输出中消除敌人。如果这是错误的,请让我知道,因为我是new在这里。谢谢。@Abubakaroloyinka这是一个黑客挑战,是一个正在进行的测试。很抱歉,在测试完成之前,我们无法进一步帮助您。当n大于115时,这会在java中产生堆栈溢出