답안 #580872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
580872 2022-06-22T03:55:15 Z Jarif_Rahman Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 165240 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 = 707;
const ll A = 976186659, B = 988144425;

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

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

    vector<vector<ll>> pref, suff;

    unordered_map<ll, int> mp;

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

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

        ll ans = mp[h1*ll(1e9)+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:26:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |             for(int j = 0; j < s.size(); j++){
      |                            ~~^~~~~~~~~~
selling_rna.cpp:40:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |             for(int j = 0; j < s.size(); j++){
      |                            ~~^~~~~~~~~~
selling_rna.cpp:57:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int i = 0; i < p.size(); i++) h1*=A, h1%=B, h1+=p[i], h1%=B;
      |                        ~~^~~~~~~~~~
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 < q.size(); i++) h2*=A, h2%=B, h2+=q[i], h2%=B;
      |                        ~~^~~~~~~~~~
selling_rna.cpp:64:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |         for(int i = 0; i < pref.size(); i++){
      |                        ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1592 ms 165240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 1004 KB Output is correct
2 Correct 53 ms 3108 KB Output is correct
3 Correct 41 ms 1948 KB Output is correct
4 Correct 22 ms 808 KB Output is correct
5 Correct 55 ms 3160 KB Output is correct
6 Correct 41 ms 1944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Execution timed out 1592 ms 165240 KB Time limit exceeded
9 Halted 0 ms 0 KB -