QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#863320 | #9747. 字符串复制 | tsai | AC ✓ | 1825ms | 46476kb | C++14 | 2.0kb | 2025-01-19 15:59:40 | 2025-01-19 15:59:50 |
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)%mod);
}
return ;
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--){
solve();
printf("\n");
}
return 0;
}
/*
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10196kb
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: 1ms
memory: 10060kb
input:
13 935330878 aabbbbababbaa
output:
348310505
result:
ok single line: '348310505'
Test #4:
score: 0
Accepted
time: 1727ms
memory: 44432kb
input:
300000 1000000000 rqwcfnskpxmdplyemxtntenuvjcbtggljuravmoaipbujlseqtuakblnqzbirxowfaeykxwldpeovyuuvjvskbllmtdlsfswoklvdpuuujryuvzbabieklgkcbqdocbmmwsjlfiqxvwocsexuldtiaucurmiewfrggbnbfmxqtaabxnbzcjnavvuuvowvyazzmgtdwvjlmwxpxontrusqgipfvfsjtckxdrloofzmjxxhlexhqcjchgryycbybcnmbjsaffaeaitkalawjuporbnzo...
output:
478922465
result:
ok single line: '478922465'
Test #5:
score: 0
Accepted
time: 5ms
memory: 10096kb
input:
3000 546279171 ddcaddabbadcdbcbacaddcabcbdcadbdadabdbadbbbbbbbcbddddbbbcdddcabcabbacadcbddbcddaabcacbcdcbccccbddababbbcacacdcaddaadacbcabbcccdbbabbddcdccabccaabbbdbaacccddccbbdbadacdcaaabbddccdbacddddaacaacbadcdacdcaddcdbcbdcbccaabadabcdcdaddaadcdadbaabacddacbcbbdaacdaccdadcdcabaacabcbdcacdacbcccdad...
output:
375109583
result:
ok single line: '375109583'
Test #6:
score: 0
Accepted
time: 47ms
memory: 10788kb
input:
20000 713238600 ddaceddaaaaaebbaccedaaaacdedadceecbcedaeabcaaecbdaebebdbdebebdddbdbabadabdddaaacdbccbccecdeaebcdabcacdbedacadcccecaddcaeeecbbabcbbccebceacdeeadbbcdcceaebaecaccbbabbceedbcedbbbbbbdeaadaaeaaecabeddbdeeaaeabcbaedbedeabbdecbacaabdadbabbdacabdcdaecaaaabecbdddeabadaacbddceecbeebcadacdbcedd...
output:
793179337
result:
ok single line: '793179337'
Test #7:
score: 0
Accepted
time: 1060ms
memory: 35552kb
input:
200000 432588167 gfeaccabbeccbfbcafcdfebabeebfgdccbgbcbddcfacccedebcagdccagafefdcbdgfcbadaeffcdaebbdadgfgdefdgbgaeeadfcebbeaaegbbcbfdeegdcfdgebddadaceaacggcgaecddfdcgbgeaffgbgfcacdddcbgaddecceaefcbddebgceeegbceceegcaaadgccdbcffebaaeddcdgbgagbbcfffaabafcagfaabfadcbadggbdcfebgbagggagdfbccdbeafgcbegfcf...
output:
68059895
result:
ok single line: '68059895'
Test #8:
score: 0
Accepted
time: 1825ms
memory: 44384kb
input:
300000 4405849 bbbabbbbaabaabaabbbaabbaaaabaaabbabbbbabbbbbabbbbababaaabbaaababbbaaabbbabbbbbaababbaabaaabbabbaaaaaabbabaaabbbbaabaabbbaaabaaaababbabbbbabbbbaaaaaaabababaaabababaabababaaaabbababbbbaabbbaabbabaabaabaaabbababbaababbaaabaaabbbababbabaababbabbabbabaabbbbbababbabaabbbaabaabbaabaababababb...
output:
147294182
result:
ok single line: '147294182'
Test #9:
score: 0
Accepted
time: 1722ms
memory: 40340kb
input:
300000 59262767 tvjfbbccnjyudccwhwkvmfqvuoxqqktkbiqqtwfdwgxcqiljnsbyelzztivlpuylpcpupeccakwmhfjtlayinlvzptznmyqgtgsfrbojfsdwpqlxyhcvhyvioaeccqqbtjbvynwprudzgbtyknbugnbhcbmwkmsfokfmbnyxmidzshdsxibmhetswuyumbiedddkibvilqlgunananvbsiukuwewuibczodpdarorttepvewabnxslnpreoitjikgfyurecxqulvbzyzqagprzybrwln...
output:
638033683
result:
ok single line: '638033683'
Test #10:
score: 0
Accepted
time: 1760ms
memory: 46388kb
input:
298942 948827395 eahqgctdomejjtldhbimwmmevmapsbmtvsrvlhhftqqnmddwgsskutelqwedplacbrkesadimqlcdqsbpqdcfotdpfwjhcdsqolungoeiwnqfbrwutdcsacmicqqlgvusplnmsqcxrxhsrpuetsmcohpntbicqpxqokpndjbkraicfmthbbxfkmcvksbmxplbcalkcskixaswxrewthtmdjhrwjdbtpqvenicfklibxnfnuwpternjevhiixjvhgvscowwjwvjocpkhbnrqxcuxvjoh...
output:
861490076
result:
ok single line: '861490076'
Test #11:
score: 0
Accepted
time: 0ms
memory: 10188kb
input:
1 114514 a
output:
114514
result:
ok single line: '114514'
Test #12:
score: 0
Accepted
time: 1ms
memory: 10192kb
input:
1 1 a
output:
1
result:
ok single line: '1'
Test #13:
score: 0
Accepted
time: 1725ms
memory: 46476kb
input:
300000 1 bceuzejmvekodaqwzluhbnyzmkedzgrzkovzvggdwzqhnjltgiezlmbzdsmhpsnvotnvwbfrstzwpmtjyewecgeevcoyeszmamvvgvwtswrpgtakoomlgmwitprmmjfeftqlrlqitcupxxugnzzihepbewitjiqinxplqehcbuyrevryzgpszslrxjcevrhrivzerjzbonqltluholuiubnpwncrkkiivtjflvkinretburctccdicprnbhrkkbgkzaidnskdizklipgnxxwuhesebzoohjcmqq...
output:
78188321
result:
ok single line: '78188321'
Test #14:
score: 0
Accepted
time: 1056ms
memory: 40336kb
input:
300000 1919810 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
954252672
result:
ok single line: '954252672'
Test #15:
score: 0
Accepted
time: 0ms
memory: 10192kb
input:
357 689696534 hdxgpbhdbxpeeyxsvxpmxflhgkgozuahtceghdqaqfrmpjnfrkwbqaorsmobamhzvymsysomqtwdbiqsplggwgskbfbbueoxlcousjgvtorvdlzziqifwmmezjwoenvvsvgeyoufzyirjvqiogufsqlfkllcaqezrvspwdepwisbdneuxjrzoybxtjhntdiiggvcprxarftgsnveyrmrbxgkcznbvuwhfvuouaiqsgtxvzlqgjpweibmpsbnkhwwqqlbzkxnkiawuehqrokqvvsonhfvrd...
output:
726994398
result:
ok single line: '726994398'
Extra Test:
score: 0
Extra Test Passed