QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#227211 | #6228. 字符串 | SorahISA | 50 | 912ms | 126556kb | C++20 | 5.1kb | 2023-10-27 02:35:41 | 2023-10-27 02:35:41 |
Judging History
answer
#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA
const int64 mod1 = 1000000007;
const int64 mod2 = 998244353;
const int64 p = 31;
struct Hash {
int64 h1, h2;
bool operator == (const Hash &rhs) const {
return (h1 == rhs.h1 and h2 == rhs.h2);
}
bool operator < (const Hash &rhs) const {
if (h1 != rhs.h1) return h1 < rhs.h1;
return h2 < rhs.h2;
}
};
void solve() {
int N, K; cin >> N >> K;
string S, T; cin >> S >> T;
vector<int> prehash1_S(N+1, 0), prehash1_T(N+1, 0), pow1_p(N+1, 1);
vector<int> prehash2_S(N+1, 0), prehash2_T(N+1, 0), pow2_p(N+1, 1);
for (int i = 1; i <= N; ++i) {
prehash1_S[i] = (1LL * prehash1_S[i-1] * p + (S[i-1] - 'a')) % mod1;
prehash2_S[i] = (1LL * prehash2_S[i-1] * p + (S[i-1] - 'a')) % mod2;
prehash1_T[i] = (1LL * prehash1_T[i-1] * p + (T[i-1] - 'a')) % mod1;
prehash2_T[i] = (1LL * prehash2_T[i-1] * p + (T[i-1] - 'a')) % mod2;
pow1_p[i] = 1LL * pow1_p[i-1] * p % mod1;
pow2_p[i] = 1LL * pow2_p[i-1] * p % mod2;
}
auto get_hash = [&](int tp, int st, int len) -> Hash {
if (tp == 0) {
return Hash{
(prehash1_S[st+len] - 1LL * prehash1_S[st] * pow1_p[len] % mod1 + mod1) % mod1,
(prehash2_S[st+len] - 1LL * prehash2_S[st] * pow2_p[len] % mod2 + mod2) % mod2
};
}
if (tp == 1) {
return Hash{
(prehash1_T[st+len] - 1LL * prehash1_T[st] * pow1_p[len] % mod1 + mod1) % mod1,
(prehash2_T[st+len] - 1LL * prehash2_T[st] * pow2_p[len] % mod2 + mod2) % mod2
};
}
return {-1, -1};
};
int64 ans = 0;
function<pair<set<int>, set<int>>(set<int>, set<int>, int, int)>
recur = [&](set<int> start_S, set<int> start_T, int kL, int kR) {
int kM = (kL + kR + 1) >> 1;
vector<pair<Hash, int>> roll_S, roll_T;
for (int x : start_S) roll_S.eb(get_hash(0, x, kM), x);
for (int x : start_T) roll_T.eb(get_hash(1, x, kM), x);
sort(ALL(roll_S)), sort(ALL(roll_T));
set<int> tmp_S, tmp_T;
int n = SZ(roll_S), m = SZ(roll_T);
if (kL == kR) {
for (int i = 0, j = 0; i < n and j < m; ++i) {
while (j < m and roll_T[j].first < roll_S[i].first) ++j;
if (roll_T[j].first == roll_S[i].first) {
start_S.erase(roll_S[i].second), start_T.erase(roll_T[j].second);
debug(roll_S[i].second, roll_T[j].second, kM);
ans += K - kM;
++j;
}
}
return pair{start_S, start_T};
}
for (int i = 0, j = 0; i < n and j < m; ) {
Hash chk = roll_S[i].first;
while (j < m and roll_T[j].first < chk) ++j;
while (i < n and roll_S[i].first == chk) {
tmp_S.ee(roll_S[i].second);
start_S.erase(roll_S[i].second);
++i;
}
while (j < m and roll_T[j].first == chk) {
tmp_T.ee(roll_T[j].second);
start_T.erase(roll_T[j].second);
++j;
}
}
auto [res_S, res_T] = recur(tmp_S, tmp_T, kM, kR);
for (int x : res_S) start_S.ee(x);
for (int x : res_T) start_T.ee(x);
return recur(start_S, start_T, kL, kM-1);
};
set<int> st_S, st_T;
for (int i = 0; i <= N-K; ++i) st_S.ee(i), st_T.ee(i);
recur(st_S, st_T, 0, K);
cout << ans << "\n";
}
int32_t main() {
fastIO();
int t = 1; // cin >> t;
for (int _ = 1; _ <= t; ++_) {
solve();
}
return 0;
}
#else
#include <bits/stdc++.h>
using namespace std;
// #define int int64_t
#define double __float80
using int64 = long long;
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;
#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())
#ifdef local
#define fastIO() void()
#define debug(...) \
fprintf(stderr, "%s", "\u001b[33m"), \
fprintf(stderr, "At [%s], line %d: (%s) = ", __FUNCTION__, __LINE__, #__VA_ARGS__), \
_do(__VA_ARGS__), \
fprintf(stderr, "%s", "\u001b[0m")
template <typename T> void _do(T &&_t) {cerr << _t << "\n";}
template <typename T, typename ...U> void _do(T &&_t, U &&..._u) {cerr << _t << ", ", _do(_u...);}
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#endif
template <typename T, typename U> bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}
template <typename T, typename U> bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}
#endif
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 0ms
memory: 3548kb
input:
11 1 rbbzzmxmbmz rbzmrbzxxxz
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
11 5 iiecieccice ecccceicicc
output:
27
result:
ok single line: '27'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
11 2 vfxaikwshqw vcrstlhgupu
output:
17
result:
ok single line: '17'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
11 5 abbaaaababb babbabbabba
output:
24
result:
ok single line: '24'
Subtask #2:
score: 10
Accepted
Test #5:
score: 10
Accepted
time: 0ms
memory: 3652kb
input:
189 183 ggzkilwnazromarxgofvbpvoiyxjezsnazromsuadvbqcrbpcgopuaflzxarxsclfzjymwofsoupxgrokklvydfclzkvzosyromseacgopuadvbqcadvbzgrovetzatvbgrgcrdfgwlyokkavyrooibhplfopwyrvkdqtplqjvetarxscoalmscxmjpzt pceqoxxlwnazromseacqiyzsbmksznaafomibhgsomseaccjgfsoupxgrokkavyromseavzzxarmprlpcgopuadvbqcrxhotzatvgt...
output:
1281
result:
ok single line: '1281'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3832kb
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: 3648kb
input:
200 186 bmtexhhnkxtturdmbdwlnojqbrgmfsldftmxfuhwcsfvhqrwtyxahyaeqhdmptdzatiusqlzplnpwwpjtqruzwzfxrkmolppethnfcjahkggzrahuhjxtzzxftapeckzluzbawtqebmkuqvowxjjejyhlwviatpwsgrjlskzdemcjaikeqodtkzjfpuxjvajbecvzbfr iuaztcsejcanhhcxbceaivostovczsbsrqongikbzzrgrmubfqexqczjgzhdonhidpecjyugbbgsalfmosipjpumrgk...
output:
2783
result:
ok single line: '2783'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3792kb
input:
200 91 babbaabaaabaabbababaaaaabababbaabbabababbabbabbaabbabbbabbaaabbbbbaaaabbbbbabbbbbbabaabbabbbbbbbabbabababbbbbbbaabbbbbabbaababbbaaababaaababbabbaaabbbbbaaaaaabababaaababbbababababbbabbbababbbababaabaa aabaabbaaaabbaaabbbbbbbbaabbbaaabbaabbabbababbabbbbbbbababbbabbababbbaaaababbbbbbbbbabaaabab...
output:
9267
result:
ok single line: '9267'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3864kb
input:
200 150 nbosezhgtlbulcqhzxmsekhmlwjxdnykhhrppccxytopamjpnmmlwjnvzxmlmzbgonxrfjnbehezhgtlbulgvjewofqnuurfjngckhhreqocuonxrlpthohchzzuatphchzwumrbokiqfbsbehnxsekhcbyoighozeesdxrbgonxrthozeebclvtpptlyjfgicbgbiko fvzctlpthoqfbssekhcbyoikzeexsezhgtlbulchhrebswwfgjjqxdnmtpotlpwcxytopauafopamjpnmzxmlmzbgon...
output:
7551
result:
ok single line: '7551'
Subtask #4:
score: 10
Accepted
Test #10:
score: 10
Accepted
time: 48ms
memory: 5876kb
input:
1970 76 dbajnykavlpizxpttpyxdtospawwemltpdearvnatpwekllfrwdelahxyilixhccmsukvusqodalaqqrxnyufonqzubxmddmryuysetyxlqrrepcajjhtoriwliaubkocbkyfwvdsuchvtxnhytznlmkzggxmolwkrwqjyhvnipugloyrpnplaokjtlwijwbdzttpeqjqdfqgtnlmkzggxmolwkccgorxwpipugloyrpnplaoubxmdoslibzcwbgqygqjmhpfbiggjanvjamjbwkcpzoucifnkjo...
output:
99038
result:
ok single line: '99038'
Test #11:
score: 0
Accepted
time: 4ms
memory: 4156kb
input:
2000 1800 vagekztgqyfqacujtmjjdggeisvtokooiolvurvumezuhipwklzgpivjrtfscnnsmbaumbnvwfrafjhrsotdfuhkafprhzrtjhjqmgduylebgxjqgmwtqocgthlxcjbxjhhpwaagzdpwmvagekztgqyfqacujtmjjdggeisvtokooiolvurvumezuhipwklzgpivjrtfscnnsmbaubvnmmftofygcvjsnotectmvyyagrpdgtcnwduojcrzwdsvqutkizcedhuqupwlrnlerheesueheorqfrf...
output:
359745
result:
ok single line: '359745'
Subtask #5:
score: 0
Wrong Answer
Test #12:
score: 10
Accepted
time: 15ms
memory: 6528kb
input:
2000 110 cctdrvumnyzeieuzynrhpgdjhtblkwegiqspeqdvlfquembvbgkbxbxqxkzcrejmjybxpaajjbahvnllhsnwqhwbodppwugaglhiccqgwfrqwrrtzvarjphzdvpanlilvvubyzfpinkncmusfpeythsyaqehcpsqeoarrjesmszoxjdwokscxiopdvlrrcatthspslqeteanbgvonavsekcjlxzqkdknewqwkzlrkogaqarlookpwmdbglitjpljgkskomjlpjaxnqneorxlvaacsepppualnwg...
output:
204737
result:
ok single line: '204737'
Test #13:
score: -10
Wrong Answer
time: 6ms
memory: 4648kb
input:
2000 1568 bbaaabbbbbabbbbbbbbabaabbbbaaabbbaaaabbbabbbbbbaabababbaabbabaabbabbabbbbbbbbbaabbaaaaaaaaaaaaababbabbbbababaaaaaaaabbababaaababaaaaaaabbbbaaaababbabbaaabaaaaabbbaabbbbabbabbbaabbbbabbababbababaaabbbaaaabbaaabbabbaaaabbbbabbbbaaabbbababbbaaabbaabaaaaabbaabbabbabaaabbaaaaabbbabaaaaababaabbb...
output:
675032
result:
wrong answer 1st lines differ - expected: '675034', found: '675032'
Subtask #6:
score: 10
Accepted
Test #15:
score: 10
Accepted
time: 130ms
memory: 25964kb
input:
136531 132287 dqkgbnnyzwmbzckupyuuupmenzjfpooutxplglbcqcyeyniuzhgecanatlvlfowlzqjrztednqbhopdrxaaephxnbfaqqfoqovicxazopnkttwzqvlyqrgistvpetfmhhughbqrncmrdbgmyfmivuxbwtuqbrqazuyygcxxgcragzwwxoxqyvpzugywirksslmhsspzountdivthtkvvdqhuoncsdrcewhqkptcwfuysrhqkgrivpckevgbejvwryjtsglmditbbjtxqubwruuqngjugbu...
output:
561550095
result:
ok single line: '561550095'
Test #16:
score: 0
Accepted
time: 912ms
memory: 126556kb
input:
150000 121497 zngvmoixlvkgnadwokornampshpamkzttdsrlrdpljvnyhtumgbjrszxmlbjvuqqilfbwzozrbrofqdiwivrtylbupfvinlggpqojtpqnyjmyisydgzwcpicsacutzkpcttcktknqmvhjvvwkomytrhajtifdqldcfnhhwaavxfqtazukudojiyhxpxqoasmlvfbgavfdamyvdapmlxjvxrzvldbthtagauvczpuofglawxdlhvfughisxfxhskuuybyipbwxauxuytsagjronxoimcbzd...
output:
3463078308
result:
ok single line: '3463078308'
Subtask #7:
score: 0
Time Limit Exceeded
Test #17:
score: 0
Time Limit Exceeded
input:
93376 10006 qvajyijbvmbtcgejthwkwatplbtmscyqqlbjmooteoyzbushasbdruqbwsvdjwzwnfzkawoaqwwibibmsnmnbinktjybibswordjyxipmonbqujxmexlgnchbiqsalrrlnupdhaxvaehcnbjsmcypcakmmengrpogkisfrqghzuakdwdyipuioibvatllelgtqaeqnnmcqcooftbyvjbrfyczwmbiuccckfgdjkdhfdnygsmpspulqwlaorrepzauxbmwpyfqzkkgjyenyfqpulqkhbuegic...
output:
result:
Subtask #8:
score: 0
Time Limit Exceeded
Test #19:
score: 0
Time Limit Exceeded
input:
90081 61832 lqqwzyiwccivztgzvdcjowmgtohjstttigxkkciexmyicxputfgudxdwemcpnnforfltonoeevwxptmbzvelckmgmboebsmfqgvxwhgdlwezreycawmjgnlinxzjsbaqjafsrwhngmorjjubzbwynwgblayrduekrmrenvcuqdmnebsjqvhgcugnwppxtbuuekizxwcpvsusxmzmhzdsmlurhzugnitxtplmsefrsihjdvroaihlluqtttthcyyuekitefhcjcrwrcricypqhehanczcqarp...
output:
result:
Subtask #9:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
input:
148067 68475 ndqltwlfnbfgjjrxfzwzlxjwuytfnmgkoozbgxmjhagcxowjeowjshohauryuvbhyijzktzzjnffhccmufbutucqpymbncwdopfziqpdcxqnkgwqekmyfxfftyzmwuyutgddadhmtteqefmbwpuauvsffbcqjhjwukvpleaykjajfdxhcspygwkikuwmtnjhjgxoaktshigoysxhrjjlptordbzoumukatnlzygzcykbnxgvlevngkwygjqrpyaodfutjhyyfygjccqkyjnzebcujwvvokd...
output:
result:
Subtask #10:
score: 0
Time Limit Exceeded
Test #23:
score: 10
Accepted
time: 114ms
memory: 23080kb
input:
142928 139024 jdgusjdfvxvfdtvgwwumkrnhgpckfgeoagwnspjyiexkkgudqtkxbeprmpvzfsdtoexmccgqmgewithttrtoceddynvnvhurikgwhvdibmyaokzysmyrjxbwymdyskhsdhnhhbhoeyzsepiyvebfyojfxzlhrxozvsvzdmlggrtbyemqcxixqnmzscepaoogdxfdrvbwxxscokuucdycppvssnckvazqoutebossriaxuhnesksxzycabccoafbluystrytufwxhhdrcfumanheulnbgph...
output:
542881204
result:
ok single line: '542881204'
Test #24:
score: -10
Time Limit Exceeded
input:
150000 101453 bbbaaabbbabbbaababaabaaaababbbbbaabaaabbababbababbaaaababbbbbaababbbabbbabaaaabaabaaaabbbbabbababbabbbaababbababaabbaababbbbbbaaaaaaabbbbaaabbababbbbbbbbbbbbabbbaaaaabaaaaabbaaabaabbbbbaabbbaabbabbaaaababaabbbaaaabbbaababaabbbaaaabaabbbabbabbaaababababbaaabaabbbabbbbbababbbbbbbbbbbaabb...