QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#865308#3844. LCS Spanning TreeSapnapTTWA 2105ms89228kbC++141.6kb2025-01-21 16:37:062025-01-21 16:37:15

Judging History

你现在查看的是最新测评结果

  • [2025-01-21 16:37:15]
  • 评测
  • 测评结果:WA
  • 用时:2105ms
  • 内存:89228kb
  • [2025-01-21 16:37:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=4e6+5;
int n;
string s,S;
unsigned long long hsh[N],p[N];
unsigned long long get(int l,int r){
	return hsh[r]-hsh[l-1]*p[r-l+1];
}
bool cmp(int x,int y){
	int l=1,r=min(n-x+1,n-y+1),res=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(get(x,x+mid-1)==get(y,y+mid-1)){
			l=mid+1;
			res=mid;
		}
		else{
			r=mid-1;
		}
	}
	return s[x+res]<s[y+res];
}
int sa[N],rk[N];
int h[N];
int ask(int x,int y){
	int l=1,r=min(n-sa[x]+1,n-sa[y]+1),res=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(get(sa[x],sa[x]+mid-1)==get(sa[y],sa[y]+mid-1)){
			res=mid;
			l=mid+1;
		}
		else{
			r=mid-1;
		}
	}
	return res;
}
int ans;
bool cmp2(int x,int y){
	return h[x]>h[y];
}
int fa[N];
int findset(int x){
	if(fa[x]==x){
		return x;
	}
	return fa[x]=findset(fa[x]);
}
int id[N],to[N];
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	int cnt=0;
	for(int i=1;i<=n;i++){
		cin>>S;
		s+=S;
		for(int j=1;j<=S.size();j++){
			to[++cnt]=i;
		}
		if(i<n){
			s+='#';
			cnt++;
		}
	}
	n=s.size();
	s=' '+s;
	p[0]=1;
	for(int i=1;i<=n;i++){
		p[i]=p[i-1]*131;
		hsh[i]=hsh[i-1]*131+s[i];
		sa[i]=i;
	}
	stable_sort(sa+1,sa+n+1,cmp);
	for(int i=2;i<=n;i++){
		h[i]=ask(i-1,i);
	}
	for(int i=1;i<=n;i++){
		id[i]=i;
		fa[i]=i;
	}
	sort(id+1,id+n+1,cmp2);
	for(int i=1,x;i<=n;i++){
		x=id[i];
		if(x==1){
			continue;
		}
		int X=findset(to[sa[x-1]]),Y=findset(to[sa[x]]);
		if(X!=Y){
			fa[X]=Y;
			ans+=h[x];
		}  
	}
	cout<<ans;
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 13880kb

input:

4
icpc
macau
regional
contest

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 14060kb

input:

3
ababa
babab
aba

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 2105ms
memory: 89228kb

input:

26
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0

result:

ok single line: '0'

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 14056kb

input:

7
jia
ran
jin
tian
chi
shen
me

output:

11

result:

wrong answer 1st lines differ - expected: '9', found: '11'