S T2 n log n求调试
  • 板块学术版
  • 楼主紊莫turtle
  • 当前回复13
  • 已保存回复13
  • 发布时间2023/10/28 18:45
  • 上次更新2023/11/2 11:01:58
查看原帖
S T2 n log n求调试
443675
紊莫turtle楼主2023/10/28 18:45
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
const int N = 1000005;

int n,ans;
string c;
string pre,suf;
gp_hash_table<string,int> cnt;
void calc(int l,int r){
	if(l==r) return ;
	int mid = (l+r) / 2;
	calc(l,mid);
	calc(mid+1,r);
	
	cnt.clear();
	
	pre = c[mid+1]; suf = c[mid];
	cnt[suf] ++;
	
	for(int i=mid-1;i>=l;i--){
		if(suf.size()&&c[i]==suf[suf.size()-1])
			suf.pop_back();
		else suf += c[i];
		cnt[suf] ++;
	}
	ans += cnt[pre];
	for(int i=mid+2;i<=r;i++){
		if(pre.size()&&c[i]==pre[pre.size()-1])
			pre.pop_back();
		else pre += c[i];
		ans += cnt[pre];
	}
}
signed main(){
	scanf("%lld",&n);
	cin>>c;
	c = " "+c;
	calc(1,n);
	printf("%lld",ans);
	return 0;
}
2023/10/28 18:45
加载中...