Non-Zero Segments

这篇博客探讨了一种使用前缀和和哈希映射来解决寻找子序列数量的问题。代码示例中,通过累加数组元素形成前缀和,并利用哈希映射记录已出现的前缀和,从而高效地找出具有相同前缀和的子序列。这种方法在处理大规模数据时具有较高的效率。
//前缀和
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
ll a[maxn];
ll sum;
ll ans;
map<ll,bool> mp;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    sum = ans = 0;
    mp[0] = true;
    a[0] = 0;
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++){
        cin>>a[i];
        //sum[i] = sum[i-1] +a[i];//前缀和
    }
    for(int i = 1;i <= n;i++){
            sum += a[i];
        if(mp.count(sum)){
            ans++;
            mp.clear();
            sum = a[i];
            mp[sum] = true;
            mp[0] = true;
        }
        else{//如果存在相同前缀和,那么就重新开始计算,因为前面已经不存在子序列为0的情况了
            mp[sum] = true;
        }
    }
    cout<<ans<<endl;
    return 0;
}

#ifndef DUBINS_H #define DUBINS_H // KEY!!!!!!!!!!! use c in c++, you need to use below code #ifndef __MATLAB__ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __MATLAB__ */ typedef enum { LSL = 0, LSR = 1, RSL = 2, RSR = 3, RLR = 4, LRL = 5 } DubinsPathType; typedef struct { /* the initial configuration */ double qi[3]; /* the lengths of the three segments */ double param[3]; /* model forward velocity / model angular velocity */ double rho; /* the path type described */ DubinsPathType type; } DubinsPath; #define EDUBOK (0) /* No error */ #define EDUBCOCONFIGS (1) /* Colocated configurations */ #define EDUBPARAM (2) /* Path parameterisitation error */ #define EDUBBADRHO (3) /* the rho value is invalid */ #define EDUBNOPATH (4) /* no connection between configurations with this word */ /** * Callback function for path sampling * * @note the q parameter is a configuration * @note the t parameter is the distance along the path * @note the user_data parameter is forwarded from the caller * @note return non-zero to denote sampling should be stopped */ typedef int (*DubinsPathSamplingCallback)(double q[3], double t, void* user_data); /** * Generate a path from an initial configuration to * a target configuration, with a specified maximum turning * radii * * A configuration is (x, y, theta), where theta is in radians, with zero * along the line x = 0, and counter-clockwise is positive * * @param path - the resultant path * @param q0 - a configuration specified as an array of x, y, theta * @param q1 - a configuration specified as an array of x, y, theta * @param rho - turning radius of the vehicle (forward velocity divided by maximum angular velocity) * @return - non-zero on error */ int dubins_shortest_path(DubinsPath* path, double q0[3], double q1[3], double rho); /** * Generate a path with a specified word from an initial configuration to * a target configuration, with a specified turning radius * * @param path - the resultant path * @param q0 - a configuration specified as an array of x, y, theta * @param q1 - a configuration specified as an array of x, y, theta * @param rho - turning radius of the vehicle (forward velocity divided by maximum angular velocity) * @param pathType - the specific path type to use * @return - non-zero on error */ int dubins_path(DubinsPath* path, double q0[3], double q1[3], double rho, DubinsPathType pathType); /** * Calculate the length of an initialised path * * @param path - the path to find the length of */ double dubins_path_length(DubinsPath* path); /** * Return the length of a specific segment in an initialized path * * @param path - the path to find the length of * @param i - the segment you to get the length of (0-2) */ double dubins_segment_length(DubinsPath* path, int i); /** * Return the normalized length of a specific segment in an initialized path * * @param path - the path to find the length of * @param i - the segment you to get the length of (0-2) */ double dubins_segment_length_normalized( DubinsPath* path, int i ); /** * Extract an integer that represents which path type was used * * @param path - an initialised path * @return - one of LSL, LSR, RSL, RSR, RLR or LRL */ DubinsPathType dubins_path_type(DubinsPath* path); /** * Calculate the configuration along the path, using the parameter t * * @param path - an initialised path * @param t - a length measure, where 0 <= t < dubins_path_length(path) * @param q - the configuration result * @returns - non-zero if 't' is not in the correct range */ int dubins_path_sample(DubinsPath* path, double t, double q[3]); /** * Walk along the path at a fixed sampling interval, calling the * callback function at each interval * * The sampling process continues until the whole path is sampled, or the callback returns a non-zero value * * @param path - the path to sample * @param stepSize - the distance along the path for subsequent samples * @param cb - the callback function to call for each sample * @param user_data - optional information to pass on to the callback * * @returns - zero on successful completion, or the result of the callback */ int dubins_path_sample_many(DubinsPath* path, double stepSize, DubinsPathSamplingCallback cb, void* user_data); /** * Convenience function to identify the endpoint of a path * * @param path - an initialised path * @param q - the configuration result */ int dubins_path_endpoint(DubinsPath* path, double q[3]); /** * Convenience function to extract a subset of a path * * @param path - an initialised path * @param t - a length measure, where 0 < t < dubins_path_length(path) * @param newpath - the resultant path */ int dubins_extract_subpath(DubinsPath* path, double t, DubinsPath* newpath); #ifndef __MATLAB__ #ifdef __cplusplus } /* extern "C" */ #endif /* __cplusplus */ #endif /* __MATLAB__ */ #endif /* DUBINS_H */ 详细注释上述代码
最新发布
09-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值