小编Eri*_*ric的帖子

最大独立集算法

我不相信存在一种算法,用于在二分图中找到最大独立顶点集,而不是在所有可能的独立集中找到最大值的强力方法.

我想知道找到所有可能的顶点集的伪代码.

假设给出了一个带有4个蓝色顶点和4个红色的二分图.目前我会的

Start with an arbitrary blue,
  find all red that don't match this blue
  put all these red in Independent Set
  find all blue that dont match these red
  put these blue in Independent Set
  Repeat for next vertex in blue

Repeat all over again for all blue then all vertices in red.
Run Code Online (Sandbox Code Playgroud)

我知道这种方式根本不能给我所有可能的独立集合组合,因为在第一步之后我选择了所有不匹配的颜色顶点而不是逐步完成所有可能性.

例如,给出具有匹配的图表

B  R
1  1
1  3 
2  1
2  3
3  1
3  3
4  2
4  4

Start with blue 1
  Choose …
Run Code Online (Sandbox Code Playgroud)

algorithm graph-theory max np independent-set

7
推荐指数
1
解决办法
5419
查看次数

C++中的2D数组文字

我正在尝试以指针指针的形式构建一个2D数组.这不起作用:

bool** data =  {
    new bool[4] {true, true, true, true},
    new bool[4] {true, false, false, true},
    new bool[4] {true, false, false, true},
    new bool[4] {true, true, true, true}
};
Run Code Online (Sandbox Code Playgroud)

可能吗?我应该怎么做?


编辑:

看起来我可能试图做错事.我有一个函数,它将一个bool未知大小的s 数组,以及整数宽度和高度作为参数.目前,签名是:

 foo(bool** data, int width, int height)
Run Code Online (Sandbox Code Playgroud)

我希望能够为数据构造一个文字,但我也需要这个函数来适用于任何大小的数组.

c++ literals multidimensional-array

7
推荐指数
1
解决办法
8077
查看次数

在Python中替换字符串时可以传递字典吗?

在PHP中,您preg_replace($patterns, $replacements, $string)可以通过传递一系列模式和替换来一次完成所有替换.

Python中的等价物是什么?

我注意到字符串和re函数replace(),sub()不要使用字典...

根据rick的评论编辑澄清:想法是将一个带有键的字典作为正则表达式模式,例如'\d+S',和(希望)常量字符串值(希望没有后向引用).现在相应地编辑我的答案(即回答实际问题).

php python regex string

6
推荐指数
1
解决办法
3452
查看次数

在Java中,在没有抛出异常之前,继续调用函数的最佳方法是什么?

在我的Java代码中,我有一个函数调用getAngle(),有时会抛出一个NoAngleException.下面的代码是编写一个函数的最佳方法,该函数一直调用getAngle()直到没有抛出异常?

public int getAngleBlocking()
{
    while(true)
    {
        int angle;
        try
        {
            angle = getAngle();
            return angle;
        }
        catch(NoAngleException e)
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者重写错误后getAngle()返回会更好NaN吗?

java exception-handling exception

6
推荐指数
2
解决办法
6223
查看次数

在2d环境中将对象从向量A移动到B,以百分比为单位

我知道向量A和B的坐标.我如何计算这两个向量之间的第一个点?第一个向量X是向量A和B之间距离的1%.所以首先我将向量A中的对象移动1%接近向量B.所以我需要计算向量X,它是对象的新向量,直到它到达向量B .

javascript vector linear-interpolation

6
推荐指数
2
解决办法
9789
查看次数

如何防止<button>缩小到其内容的大小?

我在容器中有两个元素:

<div class="container">
    <span>This is a div</span>
    <button>This is a button</button>
</div>
Run Code Online (Sandbox Code Playgroud)

风格如下:

span, button {
    display: block;
    padding: 4px;
    border: 1px solid black;
    margin: 5px;
    background: #c0c0c0;
    font: inherit;
}

.container {
    border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到现场演示.

为什么按钮的宽度与跨度不同?如何使按钮的行为类似于标准块级元素?

我需要在<button>这里使用,因为它的目的是提交表格.

html css

6
推荐指数
1
解决办法
3758
查看次数

Git pull删除了未提交的更改

我刚刚在github上创建了一个新的存储库.从一个装满文件的文件夹开始,我所做的步骤是:

git init
git add -A
git remote add origin ...

#Now pull in the first commit that github made
git pull origin master

#Check everything is OK
ls
Run Code Online (Sandbox Code Playgroud)

伊克!我的所有文件都消失了!发生了什么?我能把它们还给他们吗?

git

6
推荐指数
3
解决办法
9370
查看次数

我可以在调用纯虚函数时禁用异常吗?

我有一些看起来像这样的代码:

class Writable {
public:
    virtual void putc(const char ch) = 0;
protected:
    virtual ~Writable() {};
};

class Readable {
public:
    virtual char getc() = 0;
protected:
    virtual ~Readable() {};
};
Run Code Online (Sandbox Code Playgroud)

注意两个虚函数.使用arm-none-eabi-gcc和链接编译它(以及我的其他代码)-fno-exceptions会产生以下输出:

arm-none-eabi-size  --format=berkeley bareCortexM.elf
   text    data     bss     dec     hex filename
 108948    2304    2372  113624   1bbd8 bareCortexM.elf
Run Code Online (Sandbox Code Playgroud)

使用方法存根替代纯虚函数再次运行它会产生:

arm-none-eabi-size  --format=berkeley bareCortexM.elf
   text    data     bss     dec     hex filename
  47340    2296     304   49940    c314 bareCortexM.elf
Run Code Online (Sandbox Code Playgroud)

这种巨大的差异似乎是由于例外.有什么方法可以防止这种情况发生吗?

c++ gcc

6
推荐指数
1
解决办法
835
查看次数

在列表上迭代两次的正确方法?

在容器上执行多次迭代的正确方法是什么?从python文档:

迭代器 - 容器对象(例如列表)每次将它传递给iter()函数或在for循环中使用它时都会生成一个全新的迭代器.使用迭代器尝试此操作只会返回上一次迭代过程中使用的相同耗尽的迭代器对象,使其看起来像一个空容器.

协议的目的是一旦迭代器的next()方法引发StopIteration,它将继续在后续调用中这样做.不遵守此属性的实现被视为已损坏.(这个约束是在Python 2.3中添加的;在Python 2.2中,根据此规则会破坏各种迭代器.)

如果我有这个代码:

slist = [1,2,3,4]
rlist = reversed(slist)
list(rlist)
#[4,3,2,1]
tuple(rlist)
#()
Run Code Online (Sandbox Code Playgroud)

什么是两次迭代'rlist'的最简单,最正确的方法?

python

6
推荐指数
2
解决办法
1万
查看次数

将itertools.permutations的输出从元组列表转换为字符串列表

使用itertools置换函数后,列表中存在一些问题.

from itertools import permutations

def longestWord(letters):
    combinations = list(permutations(letters))
    for s in combinations:
        ''.join(s)
    print(combinations)

longestWord("aah")  
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

[('a', 'a', 'h'), ('a', 'h', 'a'), ('a', 'a', 'h'), ('a', 'h', 'a'), 
 ('h', 'a', 'a'), ('h', 'a', 'a')]
Run Code Online (Sandbox Code Playgroud)

我希望这是一个简单的列表,但它似乎是一个元组列表(?).任何人都可以帮助我格式化,所以它出现如下:

['aah', 'aha', 'aah', 'aha', 'haa', 'haa']
Run Code Online (Sandbox Code Playgroud)

python string tuples list permutation

6
推荐指数
1
解决办法
7969
查看次数