QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#306978#8044. Pattern Matching in A Minor "Low Space"bachbeo2007ML 260ms4092kbC++232.6kb2024-01-17 18:57:402024-01-17 18:57:41

Judging History

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

  • [2024-01-17 18:57:41]
  • 评测
  • 测评结果:ML
  • 用时:260ms
  • 内存:4092kb
  • [2024-01-17 18:57:40]
  • 提交

answer

// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/

#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define ld long double
#define pii pair<int,int>
#define piii pair<pii,int>
#define mpp make_pair
#define fi first
#define se second
const int mod=1000010233;
const int maxn=1000005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=20;
const int maxa=1000000;
const int root=3;
const int base=101;
const int B=40000;

int val[B],kmp[B],rem=1,pw=1;
int s[B],sk[B],pos[B];

void solve(){
    int n,m;cin >> n >> m;

    int hh=0;
    for(int i=1;i<=n;i++){
        char c;cin >> c;
        hh=(1LL*hh*base+c-'a'+1)%mod;
        if(i%B==0 || i==n) val[(i-1)/B+1]=hh,hh=0;
    }

    kmp[0]=-1;kmp[1]=0;
    for(int i=2;i<=(n-1)/B;i++){
        int j=kmp[i-1];
        while(j>=0 && val[j+1]!=val[i]) j=kmp[j];
        kmp[i]=j+1;
    }
    kmp[0]=0;

    int kB=(n-1)%B+1,ans=0;
    for(int i=0;i<B;i++) pw=1LL*pw*base%mod;
    for(int i=0;i<kB;i++) rem=1LL*rem*base%mod;

    int hB=0,hkB=0;
    for(int i=1;i<=m;i++){
        char c;cin >> c;
        hB=(1LL*hB*base+c-'a'+1-1LL*s[i%B]*pw%mod+mod)%mod;
        hkB=(1LL*hkB*base+c-'a'+1-1LL*sk[i%kB]*rem%mod+mod)%mod;
        s[i%B]=sk[i%kB]=c-'a'+1;
        if(i>=kB && hkB==val[(n-1)/B+1] && pos[(i-kB)%B]==(n-1)/B) ans++;
        if(pos[i%B]==(n-1)/B) pos[i%B]=kmp[pos[i%B]];
        while(pos[i%B] && val[pos[i%B]+1]!=hB) pos[i%B]=kmp[pos[i%B]];
        if(pos[i%B]+1<=(n-1)/B && val[pos[i%B]+1]==hB) pos[i%B]++;
    }
    cout << ans << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3668kb

input:

3 7
aba
abababc

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3724kb

input:

1 11
a
abracadabra

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3724kb

input:

8 10
possible
impossible

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

10 21
pleasenote
theunusualmemorylimit

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 167ms
memory: 4020kb

input:

26 9999990
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab...

output:

384615

result:

ok 1 number(s): "384615"

Test #6:

score: 0
Accepted
time: 254ms
memory: 3976kb

input:

10000000 10000000
uyuathabbfuujrxiozhvpkoimsxmujszuqgzfkueihhtlcxrpllvnqfzrxuqrxdqirrflvjiupavnyikstbleyrzmegxoeheioasjvozizhkxhdgiydaqmqkjeaklcuqywudbbekoegfhgbpfavvhxrkudmuoqpxsjzenplhaemdahlpuknsydajlxnpxrcbgevsycnqqevoscjetrsiolnqcmrvgnawzdkxovcezlocajpmfiiaiyopjibtcrbhxpiftyjtooiynlzugaewxitojg...

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 260ms
memory: 4040kb

input:

10000000 10000000
ciqpnklstdsrttjvfbctastzrdzykvumubmgsyebqgcnqtbgfbvhxhxtwvrgknjxflllieyngujwpkybtfkmikqxcncbdinkeajygklbksnkdbjhdxzwywjpcjhxxjbsuslvndtqzkwhkkaysofztupbxbtidtciwijxgzvnhcbaoprqdsvdrnrslrbvfomkffncugubztyskxdotzvlkybkzlnoflacxfjiijxajpuxcwxbjksmvrhhyieyxtkemrkkzekyratfgxuofksfvzmzpz...

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 152ms
memory: 3952kb

input:

10 10000000
aaaababbab
bbbabaababbbbbabaabbbbabaaabbaaabaaabababaabbabaaabbbaabbaaabaabaaaaaabbaaaaabbabaaaababbaaabbaaabbaababaaaaababaaaabbbbbbabaabaabbaaaaababaabbabbbbaaabbabbbbbbaaabbbabbaaaabaababbbabbbbababbaaabbbabababbaabaaaaabbaabababbbbbabbaaabbaaaaaaabbbbbbababaaababababaabbabaaaabbaabbb...

output:

9723

result:

ok 1 number(s): "9723"

Test #9:

score: 0
Accepted
time: 13ms
memory: 4092kb

input:

30842 1000000
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

29

result:

ok 1 number(s): "29"

Test #10:

score: 0
Accepted
time: 11ms
memory: 4032kb

input:

39318 1000000
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

24

result:

ok 1 number(s): "24"

Test #11:

score: -100
Memory Limit Exceeded

input:

30012 1000000
tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt...

output:

30

result: