QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#865331#3844. LCS Spanning TreeSapnapTTWA 1816ms88184kbC++142.6kb2025-01-21 16:47:522025-01-21 16:47:53

Judging History

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

  • [2025-01-21 16:47:53]
  • 评测
  • 测评结果:WA
  • 用时:1816ms
  • 内存:88184kb
  • [2025-01-21 16:47:52]
  • 提交

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){
	if(s[x]!=s[y])return s[x]<s[y];
	if(2<=min(n-x+1,n-y+1)&&s[x+1]!=s[y+1])return s[x+1]<s[y+1];
	if(3<=min(n-x+1,n-y+1)&&s[x+2]!=s[y+2])return s[x+2]<s[y+2];
	if(4<=min(n-x+1,n-y+1)&&s[x+3]!=s[y+3])return s[x+3]<s[y+3];
	if(5<=min(n-x+1,n-y+1)&&s[x+4]!=s[y+4])return s[x+4]<s[y+4];
	if(6<=min(n-x+1,n-y+1)&&s[x+5]!=s[y+5])return s[x+5]<s[y+5];
	if(7<=min(n-x+1,n-y+1)&&s[x+6]!=s[y+6])return s[x+6]<s[y+6];
	if(8<=min(n-x+1,n-y+1)&&s[x+7]!=s[y+7])return s[x+7]<s[y+7];
	if(9<=min(n-x+1,n-y+1)&&s[x+8]!=s[y+8])return s[x+8]<s[y+8];
	if(10<=min(n-x+1,n-y+1)&&s[x+9]!=s[y+9])return s[x+9]<s[y+9];
	if(11<=min(n-x+1,n-y+1)&&s[x+10]!=s[y+10])return s[x+10]<s[y+10];
	if(12<=min(n-x+1,n-y+1)&&s[x+11]!=s[y+11])return s[x+11]<s[y+11];
	if(13<=min(n-x+1,n-y+1)&&s[x+12]!=s[y+12])return s[x+12]<s[y+12];
	if(14<=min(n-x+1,n-y+1)&&s[x+13]!=s[y+13])return s[x+13]<s[y+13];
	if(15<=min(n-x+1,n-y+1)&&s[x+14]!=s[y+14])return s[x+14]<s[y+14];
	if(16<=min(n-x+1,n-y+1)&&s[x+15]!=s[y+15])return s[x+15]<s[y+15];
	int l=17,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+=char('z'+i);
			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: 1ms
memory: 14060kb

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: 1816ms
memory: 88184kb

input:

26
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0

result:

ok single line: '0'

Test #4:

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

input:

7
jia
ran
jin
tian
chi
shen
me

output:

9

result:

ok single line: '9'

Test #5:

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

input:

10
theysaynothinglastsforever
weareonlyheretoday
loveisnowornever
bringmefaraway
takemetoyourheart
takemetoyoursoul
givemeyourhandandholdme
showmewhatloveis
bemyguidingstar
itiseasytakemetoyourheart

output:

55

result:

ok single line: '55'

Test #6:

score: 0
Accepted
time: 4ms
memory: 14196kb

input:

100
dblkekaekijliimalcaidjjfaghdmhifkiebieffbddjmflkhagajcfmkccjjadgiijdbdldgbbhgcfdcadbeiabkemiefdccmhdcfilhkfabmfdmigfgigdcibgaeicedfiidgecbhdamiaiefbmbgbjhklbhafmhckklbmmiemkcbfgjihmdjkai
bciiecmbc
cdjailkkbefkbmlekiefdhklcbdccfbgkagflfemjjmkjmcgiibldlmhbcldjajgafmakfbhecgcckkkglklljhmliehidbkicm...

output:

476

result:

ok single line: '476'

Test #7:

score: -100
Wrong Answer
time: 801ms
memory: 87524kb

input:

2000
ecbhcebgbcjgjiihdefajfbbaajfjdedggciaegdiijhijgedbgejhgjjfhabdfhbihdeegcehbcjhgebcjachbdeiefejefhcjdihfcfgeegdahhjhjiiffjjadifiijjbhhjjeffabiaagcjhaachjbiecfeceefddecjchjfibgedfdghgdijdcdahfeddjihbhbbghjjffdcibaggiiadbaajhfcgdbaafbicahjhabfdbeacccfdehebciafaaffdfjdciafbhidbahdccjhjdadcciecfbhac...

output:

17773

result:

wrong answer 1st lines differ - expected: '17765', found: '17773'