上英语课获得了一种神奇做法。
#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