问题列表 - 第318064页

最长重复子数组(有重叠)

给定一个任意数字的数组,我希望快速[时间和空间复杂度至少低于 O(n\xc2\xb2),n = 数组长度]找到最长的子数组(数组中元素的连续段)重复(出现多次,或至少两次)。
\n除了所有值都是有效的 JavaScript 数字并且isFinite对于数组中的每个数字都成立之外,请不要假设数组中数字的任何界限。

\n

允许重叠,即允许子数组在其内部部分出现。例如,在数组中[1, 2, 1, 2, 1, 2]最长的重复子数组是[1, 2, 1, 2]。看这里

\n
[1, 2, 1, 2, 1, 2]\n^         ^        occurrence #1\n       ^        ^  occurrence #2\n
Run Code Online (Sandbox Code Playgroud)\n

两次出现重叠,但只要两次出现的起始索引或结束索引不同就可以。

\n

如果存在多个不同的重复子数组且它们都具有最长的长度,则答案可以是其中任何一个。然而,我怀疑任何能够找到一个答案的方法都可以同样轻松地找到所有答案。
\n此场景的一个示例是数组[1, 1, 2, 2]。子数组[1]和[2]都出现两次。其中任何一个都可以作为结果返回。

\n

最后,如果没有子数组出现多次,则答案是[](空数组)。

\n

我最近遇到了“在数组中查找重复序列”的问题,但所有答案都慢得离谱。每个人都在使用 o(n\xc2\xb3) 个解决方案,而 o(n\xc2\xb2) 中只有一个答案。

\n

我正在寻找一种可以在几秒钟内处理长度高达几十万(100,000)的数组的方法。时间复杂度至少为次二次方。如果您也能提供解决方案的 JS 示例实现,那就太好了,因为我对某些数据结构的理解不是那么扎实

\n

抱歉问了这么长的问题,如果有任何不清楚的地方请告诉我。如果您有一个想法但不是完整的解决方案,请将其放在评论中,它仍然对我有帮助。

\n

如果有帮助的话我还举了一些例子:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
大批结果
[1, 2, 5, 7, 1, …

javascript arrays algorithm time-complexity

8
推荐指数
1
解决办法
486
查看次数

是否有与 std::forward 等效的移动或复制功能,可以在 std::ranges 中完美转发?

How can I combine the following two functions into one? Is there something similar to std::forward, but for ranges?

#include <ranges>
#include <vector>
#include <algorithm>

template<class RangeIn, class RangeOut>
void moveOrCopy(RangeIn& from, RangeOut& to)
{
    std::ranges::copy(from, std::back_inserter(to));
}

template<class RangeIn, class RangeOut>
    requires std::is_rvalue_reference_v<RangeIn&&>
void moveOrCopy(RangeIn&& from, RangeOut& to)
{
    std::ranges::move(from, std::back_inserter(to));
}

void test()
{
  std::vector<int> a, b;
  moveOrCopy(a, b); // copy
  moveOrCopy(std::move(a), b); // move
}
Run Code Online (Sandbox Code Playgroud)

There is std::ranges::forward_range, but that's related to forward_iterator, not …

c++ perfect-forwarding c++20 std-ranges

5
推荐指数
3
解决办法
237
查看次数

mpsc channels stucking at the receiver

use std::sync::Arc;

use tokio::sync::mpsc;

async fn student(id : i32,tx : Arc<mpsc::Sender<String>>) {
    println!("student {} is getting their hw.",id);
    tx.send(format!("student {}'s hw !",id)).await.unwrap();
}

async fn teacher(mut rc : mpsc::Receiver<String>) -> Vec<String> {
    let mut homeworks = Vec::new();
    while let Some(hw) = rc.recv().await {
        println!("{hw}");
        homeworks.push(hw);
    }
    homeworks
}

#[tokio::main]
async fn main() {
    let (tx,rc): (mpsc::Sender<String>, mpsc::Receiver<String>) = mpsc::channel(100);
    let ch_arc: Arc<mpsc::Sender<String>> = Arc::new(tx);
    
    for i in 0..10 {
        tokio::task::spawn(student(i,ch_arc.clone()));
    }
    let hws = teacher(rc).await;
    println!("{:?}",hws);
}
Run Code Online (Sandbox Code Playgroud)

I am currently …

channel rust rust-tokio mpsc

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

What is happening when you're destructing an array without using square brackets?

Python developer here. I am using JavaScript to run the following command,

let a = [1, 2, 3];
let b , c, d = a;
console.log(b);
console.log(c);
console.log(d);
Run Code Online (Sandbox Code Playgroud)

To my surprise, b and c are undefined while d is the entire a variable. I later realize that I am meant to go let [b, c, d] = a;, however I am just trying to understand what is happening and why they're undefined.

javascript

0
推荐指数
1
解决办法
53
查看次数

让LET溢出

我有当前的公式

=LET(
    rad, ROW(C2),
    sumArea, SUM(INDIRECT(ADDRESS(rad,3)):INDIRECT(ADDRESS(rad+5,3))),
    result, sumArea/INDIRECT(ADDRESS(rad+6,2)),
    result
)
Run Code Online (Sandbox Code Playgroud)

它计算单元格 B7 中的值与更改列 C 的五天变化。在给出参数时它完美地工作ROW(C2),但我理想地希望它能够处理溢出,因为 C 列本身已溢出。当我给出参数 时ROW(C2#),它确实溢出,但在每个溢出结果中返回一个值错误。

是否无法以这种方式使用 LET 函数,或者我可以更改某些内容以使其工作?我很感谢我能得到的任何指导!:D


样本数据:

在此输入图像描述

值-28.6% 是通过取 计算得出的SUM(-2,2,2,-5,-1)/14。

excel excel-formula spill-range

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

在 IAsyncEnumerable 上调用 FIrst 然后 Skip(1) 可以吗?

我需要IAsyncEnumerable将所有结果存储在某个数据库中,但我还需要对第一项进行一些特殊处理。我是这样做的:

var someAsyncEnumerable = GetSomeAsyncEnumerable(cancellationToken);

var firstValue = await someAsyncEnumerable.FirstOrDefaultAsync(cancellationToken);

if (firstValue is not null)
{
   // log something about first value
   await HandleItem(fitstValue);
}

await foreach(var value in someAsyncEnumerable.Skip(1).WithCancellation(cancellationToken))
{
   await HandleItem(value);
}

private async Task HandleItem(Item item) 
{
   // store item in DB
}

Run Code Online (Sandbox Code Playgroud)

使用这样的代码我收到警告:

可能的多重枚举

我想知道,这只是一个在这种情况下我可以完全忽略的警告,还是我对可枚举的使用错误?

假设 myIAsyncEnumerable不应该被枚举两次,因为它效率低下,甚至会破坏。

c# .net-core iasyncenumerable

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

如何简化这些函数模板特化?

我正在处理一些遗留代码,这些代码在头文件中定义了以下模板:

template<typename T>
std::string convertToString(const T& t);
Run Code Online (Sandbox Code Playgroud)

在同一个头文件中,有一些用户定义类型的专门化。在 .cpp 文件中,有 types 的专门化int,unsigned short如下unsigned long所示:

template<>
std::string convertToString<unsigned short>(const unsigned short& s) {
    ... some implementation ...
}

template<>
std::string convertToString<int>(const int & s) {
    ... some implementation ...
}

template<>
std::string convertToString<unsigned long>(const unsigned long& value) {
    ... some implementation ...
}
Run Code Online (Sandbox Code Playgroud)

这些实现不使用 C++11 的std::to_string函数。我想简化这段代码,以便对于所有std::to_string支持的类型,即

int
long
long long
unsigned
unsigned long
unsigned long long
float
double
long double
Run Code Online (Sandbox Code Playgroud)

模板专门化使用简单的实现(例如 …

c++ templates type-traits c++11

4
推荐指数
2
解决办法
165
查看次数

如何获取函数参数列表理解的索引

Gurobipy 显然可以读取函数括号内制定的列表理解的索引。这是如何运作的?这个公式不应该将生成器对象传递给函数吗?您如何从中读取索引?

    md = gp.Model()
    md.addConstrs(True for i in [1,2,5,3])
    
Run Code Online (Sandbox Code Playgroud)

输出包含列表理解公式中使用的索引:

{1: <gurobi.Constr *Awaiting Model Update*>,
 2: <gurobi.Constr *Awaiting Model Update*>,
 5: <gurobi.Constr *Awaiting Model Update*>,
 3: <gurobi.Constr *Awaiting Model Update*>}
Run Code Online (Sandbox Code Playgroud)

python list-comprehension generator gurobi

5
推荐指数
1
解决办法
197
查看次数

如何在Delphi中使用TRegistry读取注册表值?

我正在尝试使用该类TRegistry读取 Windows 中的注册表值。

有人给我发送了一个如何使用它的示例,但代码不起作用。

uses Windows, Registry;

...

procedure TForm1.Button1Click(Sender: TObject);
var
  Registry: TRegistry;
  ProductName: string;
begin
  Registry := TRegistry.Create(KEY_READ); // or KEY_WRITE if you want to modify the value
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    ProductName := Registry.GetValue('SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'ProductName', '');
    ShowMessage('The product name is: ' + ProductName);
  finally
    Registry.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

这不是找GetValue。所以也许这不是TRegistry工作方式,或者它的工作方式在新版本的 Delphi 中已经改变了。我不确定,而且 Embarcadero docwiki 已关闭,所以我也无法在那里检查。

怎样TRegistry运作?如何使用它从注册表中读取值?

delphi registry delphi-12-athens

-1
推荐指数
1
解决办法
136
查看次数

具有八进制序列的 C++20 UTF-8 字符串文字

在将遗留代码移植到 C++20 时,我将字符串文字(带有预期的 UTF-8 编码文本)替换为 UTF-8 字符串文字(前缀为u8)。

\n

因此,我遇到了八进制序列的问题,我过去用它来逐字节编码 UTF-8 序列:

\n

虽然
\n"\\303\\274"是 的正确编码\xc3\xbc,但
\nu8"\\303\\274"最终以\xc3\x83\xc2\xbc.

\n

我对此进行了进一步调查,并在cppreference.com上发现:

\n
\n
    \n
  1. 对于每个数字转义序列,以v由转义序列中的数字序列组成的八进制或十六进制数表示的整数值给出,T 作为字符串文字\xe2\x80\x99s 数组元素类型(参见上表):

    \n
      \n
    • 如果v不超过 T 的可表示值的范围,则转义序列贡献值为 的单个代码单元v。
    • \n
    \n
  2. \n
\n
\n

(强调我的)

\n

用我自己的话说:在 UTF-8 字符串文字中,八进制 ( \\ooo) 和十六进制 ( \\xXX) 转义序列被解释为 Unicode 代码点,类似于 Unicode 序列 (\\uXXXX和\\UXXXXXXXX)。 …

c++ utf-8 c++20

3
推荐指数
1
解决办法
153
查看次数