Arrays 最短长度子集
我有一个数组和一个数字N 数组可以用数字0,1,2,3…N填充 对于eg,arr={1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0}//给定N=4 我必须找到包含所有数字1,2,…N的最小长度子数组 对于eg,上述数组的答案应该是{1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0}//length=4,并且索引开始=6,结束=9,//0基于 上述问题的一个可能答案是{1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0,0},但由于其长度为5,因此被拒绝。。 如果有多个子数组的最小长度,则答案应为1次出现。 或者,如果数组不包含1,2..N之间的一个或多个数字,则答案为“未找到子数组” 这是我的python代码。对于某些情况,它会产生错误的答案(我不知道)。。。 如果有人知道我做错了什么。Arrays 最短长度子集,arrays,search,Arrays,Search,我有一个数组和一个数字N 数组可以用数字0,1,2,3…N填充 对于eg,arr={1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0}//给定N=4 我必须找到包含所有数字1,2,…N的最小长度子数组 对于eg,上述数组的答案应该是{1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0}//length=4,并且索引开始=6,结束=9,//0基于 上述问题的一个可能答案是{1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0,0},但由于其长度为5,因此被拒绝。
shortlen=2000001 //initialise to INFINITY
shortstart=0
matchln=len(match) //match is the array containing integers
while(i<matchln):
if(match[i]>0):
leng=0
pos=[0]*n // array to keep status of found integers
j=i
start=i
sums=0
while(j<matchln and sums!=n):
if(match[j]>0):
if(pos[match[j]-1]==0): //only update status if the integer is not marked previously.
pos.pop(match[j]-1)
pos.insert(match[j]-1,1) //(match[j]-1) becuz array indexing is from 0.
sums+=1
j+=1
leng=j-i
if(j==matchln and sums!=n): // if the loop terminated,without marking all integers,that means we shouldn't proceed.
break
if(leng<shortlen): //if the length calculated is smaller then existing,then update it.
shortlen=leng
shortstart=start
i+=1
shortlen=2000001//初始化为无穷大
短启动=0
matchln=len(match)//match是包含整数的数组
而(i0):
长度=0
pos=[0]*n//保留找到的整数状态的数组
j=i
开始=i
总和=0
while(j0):
if(pos[match[j]-1]==0)://仅当整数之前未标记时更新状态。
位置pop(匹配[j]-1)
位置insert(匹配[j]-1,1)/(匹配[j]-1)因为数组索引是从0开始的。
总和+=1
j+=1
leng=j-i
if(j==matchln和sums!=n)://如果循环终止,没有标记所有整数,这意味着我们不应该继续。
打破
如果(leng一种可能性是跟踪每个起始位置的最短长度。您可以通过在阵列上进行两次传递来实现这一点:
假设对于索引1..k,您维护了一组在位置后找到的数字(在1..N范围内)(每个位置的数字不同),当您前进到位置k+1时,您需要使用位置k+1处的数字更新所有集合(*)(只要数字在1..N范围内)。一旦一个集合包含N个元素,则找到该起始位置的最短序列,记录该位置的长度
(*)认识到,对于具有完整集合的位置,您不再需要对其进行迭代。此外,一旦某个位置的集合已满,在此之前的位置集合也必须是完整的,因此您可以保留一个滑动的“开始位置”以检查集合
现在,您可以执行另一个过程,为每个位置选择最短的记录序列(您可以根据开始和序列长度计算结束位置)
status=状态//的新数组[arr.length],用于记分
//使用:set初始化状态一种可能是跟踪每个起始位置的最短长度。您可以通过在阵列上进行两次传递来完成此操作:
假设对于索引1..k,您维护了一组在位置后找到的数字(在1..N范围内)(每个位置的数字不同),当您前进到位置k+1时,您需要使用位置k+1处的数字更新所有集合(*)(只要数字在1..N范围内)。一旦一个集合包含N个元素,则找到该起始位置的最短序列,记录该位置的长度
(*)认识到,对于具有完整集合的位置,您不再需要对其进行迭代。此外,一旦某个位置的集合已满,在此之前的位置集合也必须是完整的,因此您可以保留一个滑动的“开始位置”以检查集合
现在,您可以执行另一个过程,为每个位置选择最短的记录序列(您可以根据开始和序列长度计算结束位置)
status=状态//的新数组[arr.length],用于记分
//使用:set初始化状态我想这可能会对您有所帮助。
问题的目标是将值转换为线性独立序列。我编写了一个小代码来解决问题,它可以找到所需序列的开头:
#include <stdio.h>
void main(){
/*By Volnei Klehm,
Manaus-AM, Brazil
2012
*/
long long arr[]={1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0,0};
long long power2arr[17]; /*same size or larger than arr*/
long long powerSum=0;
long long partialSum=0;
int count,count1,N;
int size_arr;
size_arr=sizeof(arr)/sizeof(long long);
/*the goal here is to find a way to represent your values as linear indepent ones,
here a sequece of power of 2 is used, you can also use other ways to do it that not increases so dramatically in values.
I use powers of 2 cause is more easy handled by computers, you can also use a sequence of sines,
cosines or any other math way that produces independets values.
For more informations I suggest you to take a look in linear algebra and/or digital signal processing books*/
/*Now it computes an independent basis*/
for(count=0 ; count<size_arr ; ++count){
power2arr[count] = 1 << arr[count]; /*calculates 2^arr generating a set of independent numbers.*/
}
N=4; /*put here your N, for n=4 it will look for*/
/*Notice that deppending on your system, N cannot be too large,
at certain point N values can make 2^N too large to be handled
by standard c/c++ types. Here is safe to use n up to 63.*/
/*now it gets the sum results of 2^0 + 2^1 + 2^2 ... + 2^N*/
++N; /* in C position starts at 0*/
for(count = 0 ; count < N ; count++)
powerSum |= 1 << count;
for(count = 0 ; count<size_arr ; ++count){
partialSum=0;
for(count1 = count ; count1 < (count + N) ; count1++){
if((count + N) > size_arr){
printf("No occurrences found!\n");
return;
}
partialSum |= power2arr[count1];
}
if(partialSum==powerSum){
printf("Ocurrence found at: %d\n", count);
return;
}
}
#包括
void main(){
/*沃尔内克莱姆,
马瑙斯阿姆河,巴西
2012
*/
长长arr[]={1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0};
长power2arr[17];/*与arr尺寸相同或更大*/
长功率um=0;
长部分=0;
整数计数,计数1,N;
int size_arr;
size_arr=sizeof(arr)/sizeof(long);
/*这里的目标是找到一种方法,将你的价值观表示为线性独立的价值观,
这里使用的是2的幂次序列,您还可以使用其他方法来实现这一点,这些方法的值不会显著增加。
我使用2的幂,因为计算机更容易处理,你也可以使用一系列正弦,
余弦或产生独立值的任何其他数学方法。
有关更多信息,我建议您阅读线性代数和/或数字信号处理书籍*/
/*现在它计算一个独立的基础*/
对于(count=0;count我想这可能会对你有所帮助。
问题的目标是将值转换为线性独立序列。我编写了一个小代码来解决问题,它可以找到所需序列的开头:
#include <stdio.h>
void main(){
/*By Volnei Klehm,
Manaus-AM, Brazil
2012
*/
long long arr[]={1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0,0};
long long power2arr[17]; /*same size or larger than arr*/
long long powerSum=0;
long long partialSum=0;
int count,count1,N;
int size_arr;
size_arr=sizeof(arr)/sizeof(long long);
/*the goal here is to find a way to represent your values as linear indepent ones,
here a sequece of power of 2 is used, you can also use other ways to do it that not increases so dramatically in values.
I use powers of 2 cause is more easy handled by computers, you can also use a sequence of sines,
cosines or any other math way that produces independets values.
For more informations I suggest you to take a look in linear algebra and/or digital signal processing books*/
/*Now it computes an independent basis*/
for(count=0 ; count<size_arr ; ++count){
power2arr[count] = 1 << arr[count]; /*calculates 2^arr generating a set of independent numbers.*/
}
N=4; /*put here your N, for n=4 it will look for*/
/*Notice that deppending on your system, N cannot be too large,
at certain point N values can make 2^N too large to be handled
by standard c/c++ types. Here is safe to use n up to 63.*/
/*now it gets the sum results of 2^0 + 2^1 + 2^2 ... + 2^N*/
++N; /* in C position starts at 0*/
for(count = 0 ; count < N ; count++)
powerSum |= 1 << count;
for(count = 0 ; count<size_arr ; ++count){
partialSum=0;
for(count1 = count ; count1 < (count + N) ; count1++){
if((count + N) > size_arr){
printf("No occurrences found!\n");
return;
}
partialSum |= power2arr[count1];
}
if(partialSum==powerSum){
printf("Ocurrence found at: %d\n", count);
return;
}
}
#包括
void main(){
/*沃尔内克莱姆,
马瑙斯阿姆河,巴西
2012
*/
长长arr[]={1,0,2,3,1,0,2,4,3,1,0,2,4,3,0,0};
长power2arr[17];/*与arr尺寸相同或更大*/
长功率um=0;
长部分=0;
整数计数,计数1,N;
int size_arr;
size_arr=sizeof(arr)/sizeof(long);
/*这里的目标是找到一种方法,将你的价值观表示为线性独立的价值观,
这里使用的是2的幂次序列,您还可以使用其他方法来实现这一点,这些方法的值不会显著增加。
我使用2的幂,因为计算机更容易处理,你也可以使用一系列正弦,
余弦或产生独立值的任何其他数学方法。
有关更多信息,我建议您阅读线性代数和/或数字信号处理书籍*/
/*现在它计算一个独立的基础*/
对于(计数=0;计数It c