求复杂度分析,关于csps2023t2
  • 板块学术版
  • 楼主Kazeno_Akina
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/10/23 12:47
  • 上次更新2023/11/2 11:44:32
查看原帖
求复杂度分析,关于csps2023t2
612567
Kazeno_Akina楼主2023/10/23 12:47

上英语课获得了一种神奇做法。

#include <bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int n,q,str[N],pre[N];
long long ans,cnt[N];
int main(){
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin >> q;
  while(q--){
    cin >> n;
    ans=0;
    for(int i=1;i<=n;++i){
      cin >> str[i];
      for(int p=i-1;p>0;p=pre[p]-1) if(str[p]==str[i]){pre[i]=p,cnt[i]=cnt[p-1]+1,ans+=cnt[i];break;}
    }
    cout << ans << '\n';
    for(int i=1;i<=n;++i) pre[i]=cnt[i]=0;
  }
  return 0;
}

求复杂度分析。想不出来qwq

2023/10/23 12:47
加载中...