QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#641910#6228. 字符串Elegia100 ✓67ms94836kbC++142.7kb2024-10-15 03:15:542024-10-15 03:15:59

Judging History

你现在查看的是最新测评结果

  • [2024-10-15 03:15:59]
  • 评测
  • 测评结果:100
  • 用时:67ms
  • 内存:94836kb
  • [2024-10-15 03:15:54]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <stack>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int N = 150010, A = 26;

namespace SAM {

struct Node {
  int fail, len;
  int ch[A];
} P[N << 2];

int root, top;

void init() {
  root = top = 1;
}

int ins(int o, int c) {
  int p = ++top;
  P[p].len = P[o].len + 1;

  while (o && !P[o].ch[c]) {
    P[o].ch[c] = p;
    o = P[o].fail;
  }
  if (!o) {
    P[p].fail = root;
  } else {
    int q = P[o].ch[c];
    if (P[q].len == P[o].len + 1) {
      P[p].fail = q;
    } else {
      int nq = ++top;
      memcpy(P + nq, P + q, sizeof(Node));
      P[nq].len = P[o].len + 1;
      P[q].fail = P[p].fail = nq;
      while (o && P[o].ch[c] == q) {
        P[o].ch[c] = nq;
        o = P[o].fail;
      }
    }
  }
  return p;
}

}

int n, k;
char s[N], t[N];
int tag[N << 2];
ll ans;
vector<int> ch[N << 2];

void dfs(int u) {
  int x0 = 0, x1 = 0;
  if (tag[u] > 0) x0 += tag[u];
  else x1 += -tag[u];
  for (int v : ch[u]) {
    dfs(v);
    if (tag[v] > 0) x0 += tag[v];
    else x1 += -tag[v];
  }
  ans -= min(k, SAM::P[u].len) * (ll)min(x0, x1);
  tag[u] = x0 - x1;
}

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k >> s >> t;
  SAM::init();
  int o = SAM::root;
  for (int i = n - 1; i >= 0; --i) {
    o = SAM::ins(o, s[i] - 'a');
    if (i <= n - k)
      tag[o] = 1;
  }
  for (int i = n - 1; i >= 0; --i) {
    o = SAM::ins(o, t[i] - 'a');
    if (i <= n - k)
      tag[o] = -1;
  }
  for (int i = 2; i <= SAM::top; ++i) {
    ch[SAM::P[i].fail].push_back(i);
  }
  ans = (n - k + 1LL) * k;
  dfs(1);
  cout << ans << '\n';

#ifdef ELEGIA
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 5ms
memory: 22260kb

input:

11 1
rbbzzmxmbmz
rbzmrbzxxxz

output:

3

result:

ok single line: '3'

Test #2:

score: 10
Accepted
time: 5ms
memory: 19888kb

input:

11 5
iiecieccice
ecccceicicc

output:

27

result:

ok single line: '27'

Test #3:

score: 10
Accepted
time: 0ms
memory: 19868kb

input:

11 2
vfxaikwshqw
vcrstlhgupu

output:

17

result:

ok single line: '17'

Test #4:

score: 10
Accepted
time: 0ms
memory: 21396kb

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: 20976kb

input:

189 183
ggzkilwnazromarxgofvbpvoiyxjezsnazromsuadvbqcrbpcgopuaflzxarxsclfzjymwofsoupxgrokklvydfclzkvzosyromseacgopuadvbqcadvbzgrovetzatvbgrgcrdfgwlyokkavyrooibhplfopwyrvkdqtplqjvetarxscoalmscxmjpzt
pceqoxxlwnazromseacqiyzsbmksznaafomibhgsomseaccjgfsoupxgrokkavyromseavzzxarmprlpcgopuadvbqcrxhotzatvgt...

output:

1281

result:

ok single line: '1281'

Test #6:

score: 10
Accepted
time: 5ms
memory: 21220kb

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: 22312kb

input:

200 186
bmtexhhnkxtturdmbdwlnojqbrgmfsldftmxfuhwcsfvhqrwtyxahyaeqhdmptdzatiusqlzplnpwwpjtqruzwzfxrkmolppethnfcjahkggzrahuhjxtzzxftapeckzluzbawtqebmkuqvowxjjejyhlwviatpwsgrjlskzdemcjaikeqodtkzjfpuxjvajbecvzbfr
iuaztcsejcanhhcxbceaivostovczsbsrqongikbzzrgrmubfqexqczjgzhdonhidpecjyugbbgsalfmosipjpumrgk...

output:

2783

result:

ok single line: '2783'

Test #8:

score: 10
Accepted
time: 0ms
memory: 20012kb

input:

200 91
babbaabaaabaabbababaaaaabababbaabbabababbabbabbaabbabbbabbaaabbbbbaaaabbbbbabbbbbbabaabbabbbbbbbabbabababbbbbbbaabbbbbabbaababbbaaababaaababbabbaaabbbbbaaaaaabababaaababbbababababbbabbbababbbababaabaa
aabaabbaaaabbaaabbbbbbbbaabbbaaabbaabbabbababbabbbbbbbababbbabbababbbaaaababbbbbbbbbabaaabab...

output:

9267

result:

ok single line: '9267'

Test #9:

score: 10
Accepted
time: 0ms
memory: 21728kb

input:

200 150
nbosezhgtlbulcqhzxmsekhmlwjxdnykhhrppccxytopamjpnmmlwjnvzxmlmzbgonxrfjnbehezhgtlbulgvjewofqnuurfjngckhhreqocuonxrlpthohchzzuatphchzwumrbokiqfbsbehnxsekhcbyoighozeesdxrbgonxrthozeebclvtpptlyjfgicbgbiko
fvzctlpthoqfbssekhcbyoikzeexsezhgtlbulchhrebswwfgjjqxdnmtpotlpwcxytopauafopamjpnmzxmlmzbgon...

output:

7551

result:

ok single line: '7551'

Subtask #4:

score: 10
Accepted

Test #10:

score: 10
Accepted
time: 4ms
memory: 22568kb

input:

1970 76
dbajnykavlpizxpttpyxdtospawwemltpdearvnatpwekllfrwdelahxyilixhccmsukvusqodalaqqrxnyufonqzubxmddmryuysetyxlqrrepcajjhtoriwliaubkocbkyfwvdsuchvtxnhytznlmkzggxmolwkrwqjyhvnipugloyrpnplaokjtlwijwbdzttpeqjqdfqgtnlmkzggxmolwkccgorxwpipugloyrpnplaoubxmdoslibzcwbgqygqjmhpfbiggjanvjamjbwkcpzoucifnkjo...

output:

99038

result:

ok single line: '99038'

Test #11:

score: 10
Accepted
time: 0ms
memory: 21864kb

input:

2000 1800
vagekztgqyfqacujtmjjdggeisvtokooiolvurvumezuhipwklzgpivjrtfscnnsmbaumbnvwfrafjhrsotdfuhkafprhzrtjhjqmgduylebgxjqgmwtqocgthlxcjbxjhhpwaagzdpwmvagekztgqyfqacujtmjjdggeisvtokooiolvurvumezuhipwklzgpivjrtfscnnsmbaubvnmmftofygcvjsnotectmvyyagrpdgtcnwduojcrzwdsvqutkizcedhuqupwlrnlerheesueheorqfrf...

output:

359745

result:

ok single line: '359745'

Subtask #5:

score: 10
Accepted

Test #12:

score: 10
Accepted
time: 4ms
memory: 21480kb

input:

2000 110
cctdrvumnyzeieuzynrhpgdjhtblkwegiqspeqdvlfquembvbgkbxbxqxkzcrejmjybxpaajjbahvnllhsnwqhwbodppwugaglhiccqgwfrqwrrtzvarjphzdvpanlilvvubyzfpinkncmusfpeythsyaqehcpsqeoarrjesmszoxjdwokscxiopdvlrrcatthspslqeteanbgvonavsekcjlxzqkdknewqwkzlrkogaqarlookpwmdbglitjpljgkskomjlpjaxnqneorxlvaacsepppualnwg...

output:

204737

result:

ok single line: '204737'

Test #13:

score: 10
Accepted
time: 3ms
memory: 21892kb

input:

2000 1568
bbaaabbbbbabbbbbbbbabaabbbbaaabbbaaaabbbabbbbbbaabababbaabbabaabbabbabbbbbbbbbaabbaaaaaaaaaaaaababbabbbbababaaaaaaaabbababaaababaaaaaaabbbbaaaababbabbaaabaaaaabbbaabbbbabbabbbaabbbbabbababbababaaabbbaaaabbaaabbabbaaaabbbbabbbbaaabbbababbbaaabbaabaaaaabbaabbabbabaaabbaaaaabbbabaaaaababaabbb...

output:

675034

result:

ok single line: '675034'

Test #14:

score: 10
Accepted
time: 0ms
memory: 21596kb

input:

2000 1344
lolkxicffjoahkfdggddidecuqrfpfooutddnzbcazpcocgubmrigntptbyvztzqgicupxqkjakctdgmbqtfljxesdkumgzypjteiomnavydabegdsueyupxucuopzhucgrbipveldsgvdzxbtskfoficlremrcxzgnhkumgkqhkgrvdabegdsueyutemrcxzgnhkbkqhylgoldsvtgbljrmcbicgqshbetgofdjcxzlysumcfxpcovccfclgjltgmjvwlgghxzvwlgghxzhanmfwhlhxsvrno...

output:

880113

result:

ok single line: '880113'

Subtask #6:

score: 10
Accepted

Test #15:

score: 10
Accepted
time: 36ms
memory: 63448kb

input:

136531 132287
dqkgbnnyzwmbzckupyuuupmenzjfpooutxplglbcqcyeyniuzhgecanatlvlfowlzqjrztednqbhopdrxaaephxnbfaqqfoqovicxazopnkttwzqvlyqrgistvpetfmhhughbqrncmrdbgmyfmivuxbwtuqbrqazuyygcxxgcragzwwxoxqyvpzugywirksslmhsspzountdivthtkvvdqhuoncsdrcewhqkptcwfuysrhqkgrivpckevgbejvwryjtsglmditbbjtxqubwruuqngjugbu...

output:

561550095

result:

ok single line: '561550095'

Test #16:

score: 10
Accepted
time: 52ms
memory: 66588kb

input:

150000 121497
zngvmoixlvkgnadwokornampshpamkzttdsrlrdpljvnyhtumgbjrszxmlbjvuqqilfbwzozrbrofqdiwivrtylbupfvinlggpqojtpqnyjmyisydgzwcpicsacutzkpcttcktknqmvhjvvwkomytrhajtifdqldcfnhhwaavxfqtazukudojiyhxpxqoasmlvfbgavfdamyvdapmlxjvxrzvldbthtagauvczpuofglawxdlhvfughisxfxhskuuybyipbwxauxuytsagjronxoimcbzd...

output:

3463078308

result:

ok single line: '3463078308'

Subtask #7:

score: 10
Accepted

Test #17:

score: 10
Accepted
time: 23ms
memory: 49800kb

input:

93376 10006
qvajyijbvmbtcgejthwkwatplbtmscyqqlbjmooteoyzbushasbdruqbwsvdjwzwnfzkawoaqwwibibmsnmnbinktjybibswordjyxipmonbqujxmexlgnchbiqsalrrlnupdhaxvaehcnbjsmcypcakmmengrpogkisfrqghzuakdwdyipuioibvatllelgtqaeqnnmcqcooftbyvjbrfyczwmbiuccckfgdjkdhfdnygsmpspulqwlaorrepzauxbmwpyfqzkkgjyenyfqpulqkhbuegic...

output:

833972344

result:

ok single line: '833972344'

Test #18:

score: 10
Accepted
time: 38ms
memory: 61256kb

input:

100000 3453
klyarstaybbaawhfordfxixwdlbxplhsequnbxtfmeanqfqimnkbcyqqyedrdtpstcddekspidolwgrqelojswgdfnaepcurhsyhvtdwfgufebqjrcyizbinzlntltteasxpwybvkiowzrdfwqybqglvtakkkejwiuvgypdtjycxiqrjfpwitfkrqkifjmxwzfpqgvvhdjfcfufqzxsnslakggtbtbzgrakleovglwbwhayczuygizjvsncrchniozxywswpchtohckqulsvytglehfhgtmk...

output:

246347135

result:

ok single line: '246347135'

Subtask #8:

score: 10
Accepted

Test #19:

score: 10
Accepted
time: 34ms
memory: 58488kb

input:

90081 61832
lqqwzyiwccivztgzvdcjowmgtohjstttigxkkciexmyicxputfgudxdwemcpnnforfltonoeevwxptmbzvelckmgmboebsmfqgvxwhgdlwezreycawmjgnlinxzjsbaqjafsrwhngmorjjubzbwynwgblayrduekrmrenvcuqdmnebsjqvhgcugnwppxtbuuekizxwcpvsusxmzmhzdsmlurhzugnitxtplmsefrsihjdvroaihlluqtttthcyyuekitefhcjcrwrcricypqhehanczcqarp...

output:

1742191016

result:

ok single line: '1742191016'

Test #20:

score: 10
Accepted
time: 48ms
memory: 71868kb

input:

100000 18330
aabbbaaaabbaabbaababababaaabbabaabbaaaabaabbbbbbababbbababaabaabbbaabbbbbbaaabbabbabbabbbbbaaabaaabbbbbabbabbabbabbaaabbaaababbaaabbaaabababbaabaabbbabaababbaabaabbbbabbabaaaabbbabbbaabbbbbaaaaabbaabbbaaaaabbaaaaaabaaabbbbabbaabbaabaaaababaaaabbbabbbaaabbbabaaaabaaababbbabaabaabaabbabbb...

output:

1438031519

result:

ok single line: '1438031519'

Subtask #9:

score: 10
Accepted

Test #21:

score: 10
Accepted
time: 47ms
memory: 67088kb

input:

148067 68475
ndqltwlfnbfgjjrxfzwzlxjwuytfnmgkoozbgxmjhagcxowjeowjshohauryuvbhyijzktzzjnffhccmufbutucqpymbncwdopfziqpdcxqnkgwqekmyfxfftyzmwuyutgddadhmtteqefmbwpuauvsffbcqjhjwukvpleaykjajfdxhcspygwkikuwmtnjhjgxoaktshigoysxhrjjlptordbzoumukatnlzygzcykbnxgvlevngkwygjqrpyaodfutjhyyfygjccqkyjnzebcujwvvokd...

output:

5449905181

result:

ok single line: '5449905181'

Test #22:

score: 10
Accepted
time: 52ms
memory: 82100kb

input:

150000 125148
ccpbdfncvudrbqqhkphhmpqgzdnpfztoddoznzptiocplmqasljuuplejyjyczoencfvaxxkwwdradqpgrxhcenakmttyisfuzksmtuhfryqrzqjbmfgoiaxnpgvkiaidndioksdtfhojgfxxnceywvkkbtdresxebwpiqqxqcwgemgjblwugmolkozcqvueobmapsrdxwllpesqiberzefaktdnxxydxorremnsjsesvvgnwugwmzkgdeathtnvqxqzkzmhnihtopfmbvaaeunnzzuiix...

output:

3100212388

result:

ok single line: '3100212388'

Subtask #10:

score: 10
Accepted

Test #23:

score: 10
Accepted
time: 51ms
memory: 80552kb

input:

142928 139024
jdgusjdfvxvfdtvgwwumkrnhgpckfgeoagwnspjyiexkkgudqtkxbeprmpvzfsdtoexmccgqmgewithttrtoceddynvnvhurikgwhvdibmyaokzysmyrjxbwymdyskhsdhnhhbhoeyzsepiyvebfyojfxzlhrxozvsvzdmlggrtbyemqcxixqnmzscepaoogdxfdrvbwxxscokuucdycppvssnckvazqoutebossriaxuhnesksxzycabccoafbluystrytufwxhhdrcfumanheulnbgph...

output:

542881204

result:

ok single line: '542881204'

Test #24:

score: 10
Accepted
time: 67ms
memory: 94836kb

input:

150000 101453
bbbaaabbbabbbaababaabaaaababbbbbaabaaabbababbababbaaaababbbbbaababbbabbbabaaaabaabaaaabbbbabbababbabbbaababbababaabbaababbbbbbaaaaaaabbbbaaabbababbbbbbbbbbbbabbbaaaaabaaaaabbaaabaabbbbbaabbbaabbabbaaaababaabbbaaaabbbaababaabbbaaaabaabbbabbabbaaababababbaaabaabbbabbbbbababbbbbbbbbbbaabb...

output:

4870040636

result:

ok single line: '4870040636'