/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Wrong Answer 1ms 200.0 KiB
#2 Wrong Answer 1ms 204.0 KiB
#3 Wrong Answer 1ms 212.0 KiB
#4 Wrong Answer 1ms 332.0 KiB
#5 Wrong Answer 5ms 400.0 KiB
#6 Wrong Answer 5ms 404.0 KiB
#7 Wrong Answer 15ms 1.109 MiB
#8 Wrong Answer 70ms 1.105 MiB
#9 Wrong Answer 63ms 1.105 MiB
#10 Wrong Answer 66ms 1.066 MiB

代码

#include<bits/stdc++.h>
#define fre(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout)
using namespace std;
const int p=31,mod=1e9+7;
int len,m,b[50050],a[50050],Pow[50050],Hash[50050],ans;
char s[50050];
bool Max(int x,int y)
{
	if(x==y) return 1;
	int l=1,r=m,tot=-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		long long hash1=((Hash[x+mid-1]-Hash[x-1]*Pow[mid]%mod)%mod+mod)%mod;
		long long hash2=((Hash[y+mid-1]-Hash[y-1]*Pow[mid]%mod)%mod+mod)%mod;
		if(hash1==hash2)
		{
			l=mid+1;
			tot=mid;
		}
		else r=mid-1;
	}
	if(tot>=m) return 1;
	return s[x+tot]<s[y+tot];
}
void merge(int l,int r)
{
	if(l==r) return;
	int mid=(l+r)>>1;
	merge(l,mid);
	merge(mid+1,r);
	int i=l,j=mid+1,k=l;
	while(i<=mid&&j<=r)
	{
		int ok=Max(a[i],a[j]);
		if(ok) b[k++]=a[i++];
		else b[k++]=a[j++],ans+=mid-i+1;
	}
	while(i<=mid) b[k++]=a[i++];
	while(j<=r) b[k++]=a[j++];
	for(int i=l;i<=r;i++) a[k]=b[k];
}
int main()
{
//	fre(sort);
	scanf("%d%d\n",&len,&m);
	scanf("%s",s+1);
	Hash[0]=0;
	for(int i=1;i<=len;i++)
	{
		Hash[i]=((Hash[i-1]*p)%mod+s[i]-'a'+1)%mod;
		Pow[i]=(Pow[i-1]*p)%mod;
		a[i]=i;
	}
	merge(1,len);
	printf("%d",ans);
	return 0;
}
/*
10 5
cbbcbabbab
37
*/

信息

递交者
类型
递交
题目
后缀数组
题目数据
下载
语言
C++
递交时间
2019-12-13 16:00:53
评测时间
2019-12-13 16:01:00
评测机
分数
0
总耗时
234ms
峰值内存
1.109 MiB