QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#797517 | #6228. 字符串 | propane | 100 ✓ | 31ms | 13336kb | C++20 | 5.7kb | 2024-12-03 09:57:57 | 2024-12-03 09:57:57 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
using LL = long long;
/*
* From https://judge.yosupo.jp/submission/52300
* Time Complexity: Suffix Array: O(N + Character_Set_Size) time and space //
128 --- ASCII
* LCP: O(N) time and space
* Usage:
* 1. Suffix Array (returns s.size() elements, NOT considering
0-length/empty suffix)
* auto sa = suffix_array(s); // s is the input string with ASCII
characters
* auto sa_wide_char = suffix_array(s, LIM); // LIM = max(s[i]) + 2,
s is the string with arbitary big characters.
* 2. LCP:
* auto lcp = LCP(s, suffix_array(s)); // returns s.size() elements,
where lcp[i]=LCP(sa[i], sa[i+1])
* Status: Tested (DMOJ: ccc03s4, SPOJ: SARRAY (100pts), Yosupo's: Suffix Array
& Number of Substrings, CodeForces EDU
*/
// Based on: Rickypon, https://judge.yosupo.jp/submission/10105
void induced_sort(const vector<int>& vec, int val_range,
vector<int>& SA, const vector<bool>& sl,
const vector<int>& lms_idx) {
vector<int> l(val_range, 0), r(val_range, 0);
for (int c : vec) {
if (c + 1 < val_range) ++l[c + 1];
++r[c];
}
partial_sum(l.begin(), l.end(), l.begin());
partial_sum(r.begin(), r.end(), r.begin());
fill(SA.begin(), SA.end(), -1);
for (int i = (int)lms_idx.size() - 1; i >= 0; --i)
SA[--r[vec[lms_idx[i]]]] = lms_idx[i];
for (int i : SA)
if (i >= 1 && sl[i - 1]) SA[l[vec[i - 1]]++] = i - 1;
fill(r.begin(), r.end(), 0);
for (int c : vec) ++r[c];
partial_sum(r.begin(), r.end(), r.begin());
for (int k = (int)SA.size() - 1, i = SA[k]; k >= 1; --k, i = SA[k])
if (i >= 1 && !sl[i - 1]) {
SA[--r[vec[i - 1]]] = i - 1;
}
}
vector<int> SA_IS(const vector<int>& vec, int val_range) {
const int n = vec.size();
vector<int> SA(n), lms_idx;
vector<bool> sl(n);
sl[n - 1] = false;
for (int i = n - 2; i >= 0; --i) {
sl[i] = (vec[i] > vec[i + 1] || (vec[i] == vec[i + 1] && sl[i + 1]));
if (sl[i] && !sl[i + 1]) lms_idx.push_back(i + 1);
}
reverse(lms_idx.begin(), lms_idx.end());
induced_sort(vec, val_range, SA, sl, lms_idx);
vector<int> new_lms_idx(lms_idx.size()), lms_vec(lms_idx.size());
for (int i = 0, k = 0; i < n; ++i)
if (!sl[SA[i]] && SA[i] >= 1 && sl[SA[i] - 1]) {
new_lms_idx[k++] = SA[i];
}
int cur = 0;
SA[n - 1] = cur;
for (size_t k = 1; k < new_lms_idx.size(); ++k) {
int i = new_lms_idx[k - 1], j = new_lms_idx[k];
if (vec[i] != vec[j]) {
SA[j] = ++cur;
continue;
}
bool flag = false;
for (int a = i + 1, b = j + 1;; ++a, ++b) {
if (vec[a] != vec[b]) {
flag = true;
break;
}
if ((!sl[a] && sl[a - 1]) || (!sl[b] && sl[b - 1])) {
flag = !((!sl[a] && sl[a - 1]) && (!sl[b] && sl[b - 1]));
break;
}
}
SA[j] = (flag ? ++cur : cur);
}
for (size_t i = 0; i < lms_idx.size(); ++i) lms_vec[i] = SA[lms_idx[i]];
if (cur + 1 < (int)lms_idx.size()) {
auto lms_SA = SA_IS(lms_vec, cur + 1);
for (size_t i = 0; i < lms_idx.size(); ++i) {
new_lms_idx[i] = lms_idx[lms_SA[i]];
}
}
induced_sort(vec, val_range, SA, sl, new_lms_idx);
return SA;
}
vector<int> suffix_array(const string& s, const char first = 1,
const char last = 122) {
vector<int> vec(s.size() + 1);
copy(begin(s), end(s), begin(vec));
for (auto& x : vec) x -= (int)first - 1;
vec.back() = 0;
auto ret = SA_IS(vec, (int)last - (int)first + 2);
ret.erase(ret.begin());
return ret;
}
// Author: https://codeforces.com/blog/entry/12796?#comment-175287
// Uses kasai's algorithm linear in time and space
vector<int> LCP(const string& s, const vector<int>& sa) {
int n = s.size(), k = 0;
vector<int> lcp(n), rank(n);
for (int i = 0; i < n; i++) rank[sa[i]] = i;
for (int i = 0; i < n; i++, k ? k-- : 0) {
if (rank[i] == n - 1) {
k = 0;
continue;
}
int j = sa[rank[i] + 1];
while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
lcp[rank[i]] = k;
}
lcp[n - 1] = 0;
return lcp;
}
const int maxn = 3e5 + 5;
int p[maxn], sz1[maxn], sz2[maxn];
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
vector<int> pos[maxn];
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, k;
cin >> n >> k;
string s1, s2;
cin >> s1 >> s2;
string s = s1 + s2;
auto sa = suffix_array(s);
auto height = LCP(s, sa);
LL sum = 1LL * k * (n - k + 1);
for(int i = 0; i < 2 * n; i++){
p[i] = i;
if (i < n and i + k - 1 < n){
sz1[i] = 1;
}
if (i >= n and i + k - 1 < 2 * n){
sz2[i] = 1;
}
}
for(int i = 0; i + 1 < 2 * n; i++){
pos[height[i]].push_back(i);
}
for(int i = 2 * n; i >= 1; i--){
for(auto x : pos[i]){
int p1 = find(sa[x]), p2 = find(sa[x + 1]);
int t1 = min(sz1[p1], sz2[p2]);
int t2 = min(sz2[p1], sz1[p2]);
sum -= 1LL * (t1 + t2) * min(i, k);
p[p1] = p2;
sz1[p2] = sz1[p1] + sz1[p2] - t1 - t2;
sz2[p2] = sz2[p1] + sz2[p2] - t1 - t2;
}
}
cout << sum << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 7680kb
input:
11 1 rbbzzmxmbmz rbzmrbzxxxz
output:
3
result:
ok single line: '3'
Test #2:
score: 10
Accepted
time: 1ms
memory: 7680kb
input:
11 5 iiecieccice ecccceicicc
output:
27
result:
ok single line: '27'
Test #3:
score: 10
Accepted
time: 1ms
memory: 7708kb
input:
11 2 vfxaikwshqw vcrstlhgupu
output:
17
result:
ok single line: '17'
Test #4:
score: 10
Accepted
time: 1ms
memory: 7900kb
input:
11 5 abbaaaababb babbabbabba
output:
24
result:
ok single line: '24'
Subtask #2:
score: 10
Accepted
Test #5:
score: 10
Accepted
time: 1ms
memory: 7648kb
input:
189 183 ggzkilwnazromarxgofvbpvoiyxjezsnazromsuadvbqcrbpcgopuaflzxarxsclfzjymwofsoupxgrokklvydfclzkvzosyromseacgopuadvbqcadvbzgrovetzatvbgrgcrdfgwlyokkavyrooibhplfopwyrvkdqtplqjvetarxscoalmscxmjpzt pceqoxxlwnazromseacqiyzsbmksznaafomibhgsomseaccjgfsoupxgrokkavyromseavzzxarmprlpcgopuadvbqcrxhotzatvgt...
output:
1281
result:
ok single line: '1281'
Test #6:
score: 10
Accepted
time: 1ms
memory: 7688kb
input:
200 57 fepkbvxoerpcvroebkmqhnffauhmrupetettegzclvsuahdpjpbudmlgqoejupzgxwudnyuiyyjgmqhgwsgsgxwpkpqjszlxyoxueypcughhvttnxzskldvfngvvbzxsetkypupmwszsexkplmgspfbzxsetkypcughgwsgstdmjpgxlfzexlxygayahepkbkmqhnkkm oexzskldvfnudxwudnmxsetkvxoerxonxzjdoejuplcvroerphpzojpqjsjlrmqjueykphydhbkszlxykmdvfyyjgwsf...
output:
7863
result:
ok single line: '7863'
Subtask #3:
score: 10
Accepted
Test #7:
score: 10
Accepted
time: 0ms
memory: 7692kb
input:
200 186 bmtexhhnkxtturdmbdwlnojqbrgmfsldftmxfuhwcsfvhqrwtyxahyaeqhdmptdzatiusqlzplnpwwpjtqruzwzfxrkmolppethnfcjahkggzrahuhjxtzzxftapeckzluzbawtqebmkuqvowxjjejyhlwviatpwsgrjlskzdemcjaikeqodtkzjfpuxjvajbecvzbfr iuaztcsejcanhhcxbceaivostovczsbsrqongikbzzrgrmubfqexqczjgzhdonhidpecjyugbbgsalfmosipjpumrgk...
output:
2783
result:
ok single line: '2783'
Test #8:
score: 10
Accepted
time: 1ms
memory: 7632kb
input:
200 91 babbaabaaabaabbababaaaaabababbaabbabababbabbabbaabbabbbabbaaabbbbbaaaabbbbbabbbbbbabaabbabbbbbbbabbabababbbbbbbaabbbbbabbaababbbaaababaaababbabbaaabbbbbaaaaaabababaaababbbababababbbabbbababbbababaabaa aabaabbaaaabbaaabbbbbbbbaabbbaaabbaabbabbababbabbbbbbbababbbabbababbbaaaababbbbbbbbbabaaabab...
output:
9267
result:
ok single line: '9267'
Test #9:
score: 10
Accepted
time: 0ms
memory: 7680kb
input:
200 150 nbosezhgtlbulcqhzxmsekhmlwjxdnykhhrppccxytopamjpnmmlwjnvzxmlmzbgonxrfjnbehezhgtlbulgvjewofqnuurfjngckhhreqocuonxrlpthohchzzuatphchzwumrbokiqfbsbehnxsekhcbyoighozeesdxrbgonxrthozeebclvtpptlyjfgicbgbiko fvzctlpthoqfbssekhcbyoikzeexsezhgtlbulchhrebswwfgjjqxdnmtpotlpwcxytopauafopamjpnmzxmlmzbgon...
output:
7551
result:
ok single line: '7551'
Subtask #4:
score: 10
Accepted
Test #10:
score: 10
Accepted
time: 1ms
memory: 7816kb
input:
1970 76 dbajnykavlpizxpttpyxdtospawwemltpdearvnatpwekllfrwdelahxyilixhccmsukvusqodalaqqrxnyufonqzubxmddmryuysetyxlqrrepcajjhtoriwliaubkocbkyfwvdsuchvtxnhytznlmkzggxmolwkrwqjyhvnipugloyrpnplaokjtlwijwbdzttpeqjqdfqgtnlmkzggxmolwkccgorxwpipugloyrpnplaoubxmdoslibzcwbgqygqjmhpfbiggjanvjamjbwkcpzoucifnkjo...
output:
99038
result:
ok single line: '99038'
Test #11:
score: 10
Accepted
time: 1ms
memory: 5796kb
input:
2000 1800 vagekztgqyfqacujtmjjdggeisvtokooiolvurvumezuhipwklzgpivjrtfscnnsmbaumbnvwfrafjhrsotdfuhkafprhzrtjhjqmgduylebgxjqgmwtqocgthlxcjbxjhhpwaagzdpwmvagekztgqyfqacujtmjjdggeisvtokooiolvurvumezuhipwklzgpivjrtfscnnsmbaubvnmmftofygcvjsnotectmvyyagrpdgtcnwduojcrzwdsvqutkizcedhuqupwlrnlerheesueheorqfrf...
output:
359745
result:
ok single line: '359745'
Subtask #5:
score: 10
Accepted
Test #12:
score: 10
Accepted
time: 1ms
memory: 5736kb
input:
2000 110 cctdrvumnyzeieuzynrhpgdjhtblkwegiqspeqdvlfquembvbgkbxbxqxkzcrejmjybxpaajjbahvnllhsnwqhwbodppwugaglhiccqgwfrqwrrtzvarjphzdvpanlilvvubyzfpinkncmusfpeythsyaqehcpsqeoarrjesmszoxjdwokscxiopdvlrrcatthspslqeteanbgvonavsekcjlxzqkdknewqwkzlrkogaqarlookpwmdbglitjpljgkskomjlpjaxnqneorxlvaacsepppualnwg...
output:
204737
result:
ok single line: '204737'
Test #13:
score: 10
Accepted
time: 1ms
memory: 5724kb
input:
2000 1568 bbaaabbbbbabbbbbbbbabaabbbbaaabbbaaaabbbabbbbbbaabababbaabbabaabbabbabbbbbbbbbaabbaaaaaaaaaaaaababbabbbbababaaaaaaaabbababaaababaaaaaaabbbbaaaababbabbaaabaaaaabbbaabbbbabbabbbaabbbbabbababbababaaabbbaaaabbaaabbabbaaaabbbbabbbbaaabbbababbbaaabbaabaaaaabbaabbabbabaaabbaaaaabbbabaaaaababaabbb...
output:
675034
result:
ok single line: '675034'
Test #14:
score: 10
Accepted
time: 1ms
memory: 7864kb
input:
2000 1344 lolkxicffjoahkfdggddidecuqrfpfooutddnzbcazpcocgubmrigntptbyvztzqgicupxqkjakctdgmbqtfljxesdkumgzypjteiomnavydabegdsueyupxucuopzhucgrbipveldsgvdzxbtskfoficlremrcxzgnhkumgkqhkgrvdabegdsueyutemrcxzgnhkbkqhylgoldsvtgbljrmcbicgqshbetgofdjcxzlysumcfxpcovccfclgjltgmjvwlgghxzvwlgghxzhanmfwhlhxsvrno...
output:
880113
result:
ok single line: '880113'
Subtask #6:
score: 10
Accepted
Test #15:
score: 10
Accepted
time: 30ms
memory: 12648kb
input:
136531 132287 dqkgbnnyzwmbzckupyuuupmenzjfpooutxplglbcqcyeyniuzhgecanatlvlfowlzqjrztednqbhopdrxaaephxnbfaqqfoqovicxazopnkttwzqvlyqrgistvpetfmhhughbqrncmrdbgmyfmivuxbwtuqbrqazuyygcxxgcragzwwxoxqyvpzugywirksslmhsspzountdivthtkvvdqhuoncsdrcewhqkptcwfuysrhqkgrivpckevgbejvwryjtsglmditbbjtxqubwruuqngjugbu...
output:
561550095
result:
ok single line: '561550095'
Test #16:
score: 10
Accepted
time: 28ms
memory: 11496kb
input:
150000 121497 zngvmoixlvkgnadwokornampshpamkzttdsrlrdpljvnyhtumgbjrszxmlbjvuqqilfbwzozrbrofqdiwivrtylbupfvinlggpqojtpqnyjmyisydgzwcpicsacutzkpcttcktknqmvhjvvwkomytrhajtifdqldcfnhhwaavxfqtazukudojiyhxpxqoasmlvfbgavfdamyvdapmlxjvxrzvldbthtagauvczpuofglawxdlhvfughisxfxhskuuybyipbwxauxuytsagjronxoimcbzd...
output:
3463078308
result:
ok single line: '3463078308'
Subtask #7:
score: 10
Accepted
Test #17:
score: 10
Accepted
time: 19ms
memory: 9848kb
input:
93376 10006 qvajyijbvmbtcgejthwkwatplbtmscyqqlbjmooteoyzbushasbdruqbwsvdjwzwnfzkawoaqwwibibmsnmnbinktjybibswordjyxipmonbqujxmexlgnchbiqsalrrlnupdhaxvaehcnbjsmcypcakmmengrpogkisfrqghzuakdwdyipuioibvatllelgtqaeqnnmcqcooftbyvjbrfyczwmbiuccckfgdjkdhfdnygsmpspulqwlaorrepzauxbmwpyfqzkkgjyenyfqpulqkhbuegic...
output:
833972344
result:
ok single line: '833972344'
Test #18:
score: 10
Accepted
time: 14ms
memory: 9576kb
input:
100000 3453 klyarstaybbaawhfordfxixwdlbxplhsequnbxtfmeanqfqimnkbcyqqyedrdtpstcddekspidolwgrqelojswgdfnaepcurhsyhvtdwfgufebqjrcyizbinzlntltteasxpwybvkiowzrdfwqybqglvtakkkejwiuvgypdtjycxiqrjfpwitfkrqkifjmxwzfpqgvvhdjfcfufqzxsnslakggtbtbzgrakleovglwbwhayczuygizjvsncrchniozxywswpchtohckqulsvytglehfhgtmk...
output:
246347135
result:
ok single line: '246347135'
Subtask #8:
score: 10
Accepted
Test #19:
score: 10
Accepted
time: 14ms
memory: 11092kb
input:
90081 61832 lqqwzyiwccivztgzvdcjowmgtohjstttigxkkciexmyicxputfgudxdwemcpnnforfltonoeevwxptmbzvelckmgmboebsmfqgvxwhgdlwezreycawmjgnlinxzjsbaqjafsrwhngmorjjubzbwynwgblayrduekrmrenvcuqdmnebsjqvhgcugnwppxtbuuekizxwcpvsusxmzmhzdsmlurhzugnitxtplmsefrsihjdvroaihlluqtttthcyyuekitefhcjcrwrcricypqhehanczcqarp...
output:
1742191016
result:
ok single line: '1742191016'
Test #20:
score: 10
Accepted
time: 12ms
memory: 9632kb
input:
100000 18330 aabbbaaaabbaabbaababababaaabbabaabbaaaabaabbbbbbababbbababaabaabbbaabbbbbbaaabbabbabbabbbbbaaabaaabbbbbabbabbabbabbaaabbaaababbaaabbaaabababbaabaabbbabaababbaabaabbbbabbabaaaabbbabbbaabbbbbaaaaabbaabbbaaaaabbaaaaaabaaabbbbabbaabbaabaaaababaaaabbbabbbaaabbbabaaaabaaababbbabaabaabaabbabbb...
output:
1438031519
result:
ok single line: '1438031519'
Subtask #9:
score: 10
Accepted
Test #21:
score: 10
Accepted
time: 31ms
memory: 12428kb
input:
148067 68475 ndqltwlfnbfgjjrxfzwzlxjwuytfnmgkoozbgxmjhagcxowjeowjshohauryuvbhyijzktzzjnffhccmufbutucqpymbncwdopfziqpdcxqnkgwqekmyfxfftyzmwuyutgddadhmtteqefmbwpuauvsffbcqjhjwukvpleaykjajfdxhcspygwkikuwmtnjhjgxoaktshigoysxhrjjlptordbzoumukatnlzygzcykbnxgvlevngkwygjqrpyaodfutjhyyfygjccqkyjnzebcujwvvokd...
output:
5449905181
result:
ok single line: '5449905181'
Test #22:
score: 10
Accepted
time: 27ms
memory: 12520kb
input:
150000 125148 ccpbdfncvudrbqqhkphhmpqgzdnpfztoddoznzptiocplmqasljuuplejyjyczoencfvaxxkwwdradqpgrxhcenakmttyisfuzksmtuhfryqrzqjbmfgoiaxnpgvkiaidndioksdtfhojgfxxnceywvkkbtdresxebwpiqqxqcwgemgjblwugmolkozcqvueobmapsrdxwllpesqiberzefaktdnxxydxorremnsjsesvvgnwugwmzkgdeathtnvqxqzkzmhnihtopfmbvaaeunnzzuiix...
output:
3100212388
result:
ok single line: '3100212388'
Subtask #10:
score: 10
Accepted
Test #23:
score: 10
Accepted
time: 25ms
memory: 12864kb
input:
142928 139024 jdgusjdfvxvfdtvgwwumkrnhgpckfgeoagwnspjyiexkkgudqtkxbeprmpvzfsdtoexmccgqmgewithttrtoceddynvnvhurikgwhvdibmyaokzysmyrjxbwymdyskhsdhnhhbhoeyzsepiyvebfyojfxzlhrxozvsvzdmlggrtbyemqcxixqnmzscepaoogdxfdrvbwxxscokuucdycppvssnckvazqoutebossriaxuhnesksxzycabccoafbluystrytufwxhhdrcfumanheulnbgph...
output:
542881204
result:
ok single line: '542881204'
Test #24:
score: 10
Accepted
time: 31ms
memory: 13336kb
input:
150000 101453 bbbaaabbbabbbaababaabaaaababbbbbaabaaabbababbababbaaaababbbbbaababbbabbbabaaaabaabaaaabbbbabbababbabbbaababbababaabbaababbbbbbaaaaaaabbbbaaabbababbbbbbbbbbbbabbbaaaaabaaaaabbaaabaabbbbbaabbbaabbabbaaaababaabbbaaaabbbaababaabbbaaaabaabbbabbabbaaababababbaaabaabbbabbbbbababbbbbbbbbbbaabb...
output:
4870040636
result:
ok single line: '4870040636'