Submission #1106547

#TimeUsernameProblemLanguageResultExecution timeMemory
1106547duyhoanhoSelling RNA Strands (JOI16_selling_rna)C++17
100 / 100
351 ms712008 KiB
#include<bits/stdc++.h> #define forinc(i,a,b) for(int i=a;i<=b;i++) #define fordec(i,a,b) for(int i=a;i>=b;i--) #define pii pair<int,int> #define fi first #define se second #define pb push_back using namespace std; const int N = 3e6+10; int n,m; string a[N]; struct trie { int child[26]; int exist,cnt; int l,r; } node[N]; int cur; void st() { memset(node[0].child , -1 ,sizeof node[0].child); node[0].cnt = node[0].exist = node[0].l = node[0].r = 0 ; } int new_(int i) { cur++; memset(node[cur].child , -1 ,sizeof node[cur].child); node[cur].cnt = node[cur].exist = 0 ; node[cur].l = node[cur].r = i; return cur; } void ad(string s,int i) { int pos = 0; for(auto f : s) { int c = f - 'A'; if(node[pos].child[c] == -1) node[pos].child[c] = new_(i); pos = node[pos].child[c]; // node[pos].cnt++; node[pos].r = i; } //node[pos].exist++; } struct tir { int chil[26]; vector<int> v; }no[N]; int cur2; void st2() { memset(no[0].chil , -1 , sizeof no[0].chil); no[0].v.clear(); } int new_2(int i) { cur2++; memset(no[cur2].chil , -1 , sizeof no[cur2].chil); no[cur2].v.clear(); return cur2; } void ad2(string s , int i) { // cout<<s<<"\n"; int pos = 0; for(auto f : s) { int c = f - 'A'; if(no[pos].chil[c] == -1) no[pos].chil[c] = new_2(i); // if(c == 'A' - 'A' && pos == 0) cerr<<pos<<" "<<no[pos].chil[c]<<"\n"; pos = no[pos].chil[c]; no[pos].v.pb(i); // cerr<<pos<<"\n"; } // cerr<<"\n"; } void fin(string s , int &l , int &r) { int pos = 0; for(auto f : s) { int c = f - 'A'; if(node[pos].child[c] == -1) { l = -1; r = -1; return; } pos = node[pos].child[c]; } l = node[pos].l ; r = node[pos].r ; } void fin2(string s , int l , int r) { int pos = 0; for(auto f : s) { int c = f - 'A'; if(no[pos].chil[c] == -1) { cout<<0<<"\n"; return; } pos = no[pos].chil[c]; } // cerr<<s<<" "<<l<<" "<<r<<"\n"; // for(auto k : no[pos].v) cerr<<k<<" "; // cerr<<"\n"; int ll = lower_bound(no[pos].v.begin() , no[pos].v.end() , l) - no[pos].v.begin(); int rr = upper_bound(no[pos].v.begin() , no[pos].v.end() , r) - no[pos].v.begin(); cout<<rr - ll<<"\n"; } int32_t main() { #define task "task" if(fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>m; forinc(i,1,n) cin>>a[i]; sort(a+1,a+n+1); st(); forinc(i,1,n) ad(a[i],i); st2(); forinc(i,1,n) { string s = a[i]; reverse(s.begin() , s.end()); ad2(s,i); } forinc(i,0,cur2 - 1) { sort(no[i].v.begin() , no[i].v.end()); // no[i].v.resize(unique(no[i].v.begin() , no[i].v.end()) - no[i].v.begin()); } forinc(i,1,m) { string s1,s2; cin>>s1>>s2; reverse(s2.begin(),s2.end()); int l,r; fin(s1,l,r); //cout<<l<<" "<<r<<"\n"; if(l == -1) { cout<<0<<"\n"; continue; } fin2(s2,l,r); } }

Compilation message (stderr)

selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...