请求加强数据
查看原帖
请求加强数据
684254
Rain_chr楼主2023/5/26 14:06

复杂度带log的同学们注意了,其实双log也能过!

大致思想是stable_sort套上二分哈希值

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
char s[N];
#define ull unsigned long long 
ull h[N],Pow[N],base=131;
int a[N];
int n;
ull f(int l,int r)
{
	return h[r]-h[l-1]*Pow[r-l+1];
}
bool cmp(int x,int y)
{
	bool op=0;
	if(x>y)
		op=1,swap(x,y);
	int l=1,r=y-x,ans=0; //后面相同的位数
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(f(x+1,x+mid)==f(x,x+mid-1))
			ans=mid,l=mid+1;
		else
			r=mid-1;	
	} 
	if(x+ans==y)
		return (x<y)^op;
	else
		return (s[x+ans+1]<s[x+ans])^op;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0); 
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>s[i];
	Pow[0]=1;
	for(int i=1;i<=n;i++)
		Pow[i]=Pow[i-1]*base;
	for(int i=1;i<=n;i++)
		h[i]=h[i-1]*base+s[i];
	for(int i=1;i<=n;i++)
		a[i]=i;
	stable_sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	return 0;
}
2023/5/26 14:06
加载中...