Submission #580874

# Submission time Handle Problem Language Result Execution time Memory
580874 2022-06-22T04:03:38 Z Jarif_Rahman Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 170588 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int sq = 400;
const int A = 976186659;
const ll B = 988144425;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m; cin >> n >> m;

    vector<vector<int>> pref, suff;

    unordered_map<ll, int> mp;

    for(int i = 0; i < n; i++){
        str s; cin >> s;
        if(s.size() < sq){
            int h = 0;
            for(int j = 0; j < s.size(); j++){
                h = (ll(h)*A)%B;
                h = (ll(h)+s[j])%B;
                int hh = 0, p = 1;
                for(int k = int(s.size())-1; k >= 0; k--){
                    hh = (ll(p)*s[k]+hh)%B;
                    p = (ll(p)*A)%B;
                    mp[ll(1e9)*h+hh]++;
                }
            }
        }
        else{
            pref.pb(vector<int>(s.size())), suff.pb(vector<int>(s.size()));
            int h = 0;
            for(int j = 0; j < s.size(); j++){
                h = (ll(h)*A)%B;
                h = (ll(h)+s[j])%B;
                pref.back()[j] = h;
            }
            h = 0; int p = 1;
            for(int j = int(s.size())-1; j >= 0; j--){
                h = (ll(p)*s[j]+h)%B;
                p = (ll(p)*A)%B;
                suff.back()[j] = h;
            }
        }
    }

    while(m--){
        str p, q; cin >> p >> q;
        int h1 = 0, h2 = 0;
        for(int i = 0; i < p.size(); i++) h1 = (ll(h1)*A)%B, h1 = (ll(h1)+p[i])%B;
        for(int i = 0; i < q.size(); i++) h2 = (ll(h2)*A)%B, h2 = (ll(h2)+q[i])%B;

        int ans = mp[ll(1e9)*h1+h2];

        int psz = p.size(), qsz = q.size();

        for(int i = 0; i < pref.size(); i++){
            int sz = pref[i].size();
            if(max(psz, qsz) <= sz && pref[i][psz-1] == h1 && suff[i][sz-qsz] == h2) ans++;
        }

        cout << ans << "\n";
    }
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:27:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |             for(int j = 0; j < s.size(); j++){
      |                            ~~^~~~~~~~~~
selling_rna.cpp:41:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |             for(int j = 0; j < s.size(); j++){
      |                            ~~^~~~~~~~~~
selling_rna.cpp:58:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for(int i = 0; i < p.size(); i++) h1 = (ll(h1)*A)%B, h1 = (ll(h1)+p[i])%B;
      |                        ~~^~~~~~~~~~
selling_rna.cpp:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         for(int i = 0; i < q.size(); i++) h2 = (ll(h2)*A)%B, h2 = (ll(h2)+q[i])%B;
      |                        ~~^~~~~~~~~~
selling_rna.cpp:65:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         for(int i = 0; i < pref.size(); i++){
      |                        ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1592 ms 170588 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 596 KB Output is correct
2 Correct 67 ms 2856 KB Output is correct
3 Correct 37 ms 1576 KB Output is correct
4 Correct 20 ms 468 KB Output is correct
5 Correct 50 ms 2820 KB Output is correct
6 Correct 39 ms 1568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Execution timed out 1592 ms 170588 KB Time limit exceeded
9 Halted 0 ms 0 KB -