QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#865318 | #3844. LCS Spanning Tree | SapnapTT | WA | 2985ms | 87732kb | C++14 | 1.6kb | 2025-01-21 16:41:14 | 2025-01-21 16:41:15 |
Judging History
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+=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: 0ms
memory: 14056kb
input:
4 icpc macau regional contest
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 0ms
memory: 14056kb
input:
3 ababa babab aba
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 2030ms
memory: 87732kb
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: 13992kb
input:
100 dblkekaekijliimalcaidjjfaghdmhifkiebieffbddjmflkhagajcfmkccjjadgiijdbdldgbbhgcfdcadbeiabkemiefdccmhdcfilhkfabmfdmigfgigdcibgaeicedfiidgecbhdamiaiefbmbgbjhklbhafmhckklbmmiemkcbfgjihmdjkai bciiecmbc cdjailkkbefkbmlekiefdhklcbdccfbgkagflfemjjmkjmcgiibldlmhbcldjajgafmakfbhecgcckkkglklljhmliehidbkicm...
output:
476
result:
ok single line: '476'
Test #7:
score: -100
Wrong Answer
time: 2985ms
memory: 87584kb
input:
2000 ecbhcebgbcjgjiihdefajfbbaajfjdedggciaegdiijhijgedbgejhgjjfhabdfhbihdeegcehbcjhgebcjachbdeiefejefhcjdihfcfgeegdahhjhjiiffjjadifiijjbhhjjeffabiaagcjhaachjbiecfeceefddecjchjfibgedfdghgdijdcdahfeddjihbhbbghjjffdcibaggiiadbaajhfcgdbaafbicahjhabfdbeacccfdehebciafaaffdfjdciafbhidbahdccjhjdadcciecfbhac...
output:
17650
result:
wrong answer 1st lines differ - expected: '17765', found: '17650'