Submission #580872

#TimeUsernameProblemLanguageResultExecution timeMemory
580872Jarif_RahmanSelling RNA Strands (JOI16_selling_rna)C++17
35 / 100
1592 ms165240 KiB
#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 (stderr)

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++){
      |                        ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...