相关疑难解决方法(0)

如何有效地找到一定宽度的字符串的理想列数?

我有Ñ的不同长度的字符串小号1,S 2,...,S Ñ我要显示在终端上Ç列.终端的宽度为m个字符.每列i具有一定宽度w i,其等于该列中最长条目的宽度.每对列之间有空间的一定量的小号.包括空间之间的所有列的总宽度不能大于端子的宽度(w 1 + w 2 + ... + w c +(c-1)·s≤m).每列应包含⌈n / c⌉字符串,除非n不能由c均匀分割,在这种情况下,最后几列应缩短一个条目,或者只有最后一列应短,取决于字符串是否排列横向或向下.

是否存在有效的(例如O(n·w),其中w = max(w 1,w 2,...,w n))算法,以计算出我可以适应c列的最大列数,如果......

language-agnostic algorithm formatting multiple-columns

11
推荐指数
1
解决办法
422
查看次数

缓存友好的离线随机读取

考虑一下 C++ 中的这个函数:

void foo(uint32_t *a1, uint32_t *a2, uint32_t *b1, uint32_t *b2, uint32_t *o) {
    while (b1 != b2) {
        // assert(0 <= *b1 && *b1 < a2 - a1)
        *o++ = a1[*b1++];
    }
}
Run Code Online (Sandbox Code Playgroud)

其目的应该足够明确。不幸的是,b1包含随机数据并垃圾缓存,成为foo我的程序的瓶颈。无论如何我可以优化它吗?

这是一个 SSCCE,应该类似于我的实际代码:

#include <iostream>
#include <chrono>
#include <algorithm>
#include <numeric>

namespace {
    void foo(uint32_t *a1, uint32_t *a2, uint32_t *b1, uint32_t *b2, uint32_t *o) {
        while (b1 != b2) {
            // assert(0 <= *b1 && *b1 < a2 - …
Run Code Online (Sandbox Code Playgroud)

algorithm optimization performance x86 cpu-cache

4
推荐指数
1
解决办法
410
查看次数