QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#863317 | #9747. 字符串复制 | tsai | WA | 1670ms | 44384kb | C++14 | 2.0kb | 2025-01-19 15:59:07 | 2025-01-19 15:59:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int N=1300005;
string s;
ll n,sa[N]={0}, rk[N << 1]={0}, oldrk[N << 1]={0},height[N<<1]={0};
//height:第i名的后缀与i-1名的最长公共前缀
int w;//每次“跳的号”
void SA(){
// cin>>s;
n=s.length();
for(int i=1;i<=n;i++){//初始时是单字符
rk[i]=s[i-1];//字符相同的时候初始排名相同
sa[i]=i;//初始化都不同即可
height[i]=0;
}
for(w=1;w<n;w*=2){
// sort(sa+1,sa+n+1,cmp);
sort(sa+1,sa+n+1,[](int x,int y)->bool{return rk[x]==rk[y]?rk[x+w]<rk[y+w]:rk[x]<rk[y];});
for(int j=1;j<=n+w;j++){
oldrk[j]=rk[j];//下面要覆盖,这里先copy一份
}
for(int p=0,i=1;i<=n;i++){
if(oldrk[sa[i]]==oldrk[sa[i-1]]&&oldrk[sa[i]+w]==oldrk[sa[i-1]+w]){
//排名为i的左右两半和排名为i-1的左右两半都一样,排名一样
rk[sa[i]]=p;
}else{
rk[sa[i]]=++p;
}
}
}
//处理完的sa[i]=j储存了排名为i的后缀起点是j(即标号是j)
//求height,相邻位次的字符串只会差一个
for (int i=1,k=0;i<=n;i++) {
if (rk[i] == 0) continue;
if (k) --k;
while (s[i + k-1] == s[sa[rk[i] - 1] + k-1]) ++k;//字符串访问记的位置-1
height[rk[i]] = k;
}
return ;
}
void solve(){
ll m;
scanf("%lld %lld",&n,&m);
string tmp="";
ll d=n;
cin>>s;
tmp=s;
SA();
ll ans=(ll)(n+1)*(ll)n/(ll)2;//总的
for(int i=1;i<=n;i++){//重复的
ans-=height[i];
}
ans%=mod;
n+=d;
ll ans2=(ll)(n+1)*(ll)n/(ll)2;
s=s+tmp;
SA();
for(int i=1;i<=n;i++){//重复的
ans2-=height[i];
}
ans2%=mod;
s=s+tmp;
n+=d;
ll ans3=(ll)(n+1)*(ll)n/(ll)2;
SA();
for(int i=1;i<=n;i++){//重复的
ans3-=height[i];
}
ans3%=mod;
ll res=(ans3-ans2+2ll*mod)%mod;
if(m==1){
printf("%lld",ans);
}else if(m==2){
printf("%lld",ans2);
}else{
printf("%lld",ans2+((m-2)%mod*res%mod)%mod);
}
return ;
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--){
solve();
printf("\n");
}
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10068kb
input:
6 2 mantle
output:
57
result:
ok single line: '57'
Test #2:
score: 0
Accepted
time: 0ms
memory: 10192kb
input:
12 1919810 ifamjlifamjl
output:
138226305
result:
ok single line: '138226305'
Test #3:
score: 0
Accepted
time: 0ms
memory: 10028kb
input:
13 935330878 aabbbbababbaa
output:
348310505
result:
ok single line: '348310505'
Test #4:
score: 0
Accepted
time: 1670ms
memory: 44384kb
input:
300000 1000000000 rqwcfnskpxmdplyemxtntenuvjcbtggljuravmoaipbujlseqtuakblnqzbirxowfaeykxwldpeovyuuvjvskbllmtdlsfswoklvdpuuujryuvzbabieklgkcbqdocbmmwsjlfiqxvwocsexuldtiaucurmiewfrggbnbfmxqtaabxnbzcjnavvuuvowvyazzmgtdwvjlmwxpxontrusqgipfvfsjtckxdrloofzmjxxhlexhqcjchgryycbybcnmbjsaffaeaitkalawjuporbnzo...
output:
478922465
result:
ok single line: '478922465'
Test #5:
score: 0
Accepted
time: 6ms
memory: 10096kb
input:
3000 546279171 ddcaddabbadcdbcbacaddcabcbdcadbdadabdbadbbbbbbbcbddddbbbcdddcabcabbacadcbddbcddaabcacbcdcbccccbddababbbcacacdcaddaadacbcabbcccdbbabbddcdccabccaabbbdbaacccddccbbdbadacdcaaabbddccdbacddddaacaacbadcdacdcaddcdbcbdcbccaabadabcdcdaddaadcdadbaabacddacbcbbdaacdaccdadcdcabaacabcbdcacdacbcccdad...
output:
375109583
result:
ok single line: '375109583'
Test #6:
score: 0
Accepted
time: 46ms
memory: 10784kb
input:
20000 713238600 ddaceddaaaaaebbaccedaaaacdedadceecbcedaeabcaaecbdaebebdbdebebdddbdbabadabdddaaacdbccbccecdeaebcdabcacdbedacadcccecaddcaeeecbbabcbbccebceacdeeadbbcdcceaebaecaccbbabbceedbcedbbbbbbdeaadaaeaaecabeddbdeeaaeabcbaedbedeabbdecbacaabdadbabbdacabdcdaecaaaabecbdddeabadaacbddceecbeebcadacdbcedd...
output:
793179337
result:
ok single line: '793179337'
Test #7:
score: -100
Wrong Answer
time: 994ms
memory: 35540kb
input:
200000 432588167 gfeaccabbeccbfbcafcdfebabeebfgdccbgbcbddcfacccedebcagdccagafefdcbdgfcbadaeffcdaebbdadgfgdefdgbgaeeadfcebbeaaegbbcbfdeegdcfdgebddadaceaacggcgaecddfdcgbgeaffgbgfcacdddcbgaddecceaefcbddebgceeegbceceegcaaadgccdbcffebaaeddcdgbgagbbcfffaabafcagfaabfadcbadggbdcfebgbagggagdfbccdbeafgcbegfcf...
output:
1066304248
result:
wrong answer 1st lines differ - expected: '68059895', found: '1066304248'