#50604: 使用類似互等的爬梯比較,也可以很快


kk20180820@gmail.com (Wayne Yang)

學校 : 國立鳳山高級中學
編號 : 172018
來源 : [49.159.255.92]
最後登入時間 :
2025-08-10 14:18:00

儲存好 前綴和 和 後綴和 陣列後,
使用兩個index,
當A陣列的數值比較大,
就增加B陣列的index,
等到B陣列的數值超越自己時,
再換成A陣列的index增加,
重複直到有一邊的index值>=總個數n。
for(preindex=sufindex=0;preindex<n && sufindex<n;){
    if(pre[preindex]<suf[sufindex]) ++preindex;
    else if(pre[preindex]>suf[sufindex]) ++sufindex;
    else{//pre[preindex]==suf[sufindex]
        ++preindex;
        ++sufindex;
        ++ans;
    }
}