Submission #1278588

#TimeUsernameProblemLanguageResultExecution timeMemory
1278588trinm01Selling RNA Strands (JOI16_selling_rna)C++20
35 / 100
1603 ms210196 KiB
// #pragma GCC optimize("O3")
// #pragma GCC optimization("Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;

#define int long long 
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>

const ll mod = 1e9 + 7;
const int MAXN = 1e5 + 5;
const ll oo = 1e18 + 7;  
const int base = 31;
const int B = 200;

int n, q;
vector<unsigned long long> h[MAXN];
unsigned long long mu[MAXN];

unsigned long long get(vector<unsigned long long> &h, int l, int r){
	return h[r]-h[l-1]*mu[r-l+1];
}

vector<pii> heavy;

unordered_map<unsigned long long, int> mp;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    if(fopen(".inp", "r")){
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    
    mu[0]=1;
    FOR(i, 1, 100000){
    	mu[i]=mu[i-1]*base;
    }

    cin >> n >> q;
    FOR(i, 1, n){
    	string s;
    	cin >> s;
    	s="."+s;
    	h[i].resize((int)s.size());
    	FOR(j, 1, (int)s.size()-1){
    		h[i][j]=h[i][j-1]*base + (unsigned long long)(s[j]-'a'+1);
    	}
    	
    	if((int)s.size()>B){
    		heavy.push_back({i, (int)s.size()-1});
    	}
    	else{
    		FOR(jj, 1, (int)s.size()-1){
    			FOR(j, 1, (int)s.size()-1){
    				unsigned long long x=get(h[i], 1, jj);
    				unsigned long long y=get(h[i], j, (int)s.size()-1);
    				unsigned long long key = (x<<32) ^ (y & 0xffffffffULL);
    				mp[key]++;	
    			}
    		}
    	}
    }
    
    while(q--){
    	string a, b;
    	cin >> a >> b;
    	unsigned long long sum1=0;
    	for(auto x:a){
    		sum1 = sum1*base + (unsigned long long)(x-'a'+1);
    	}	
    	unsigned long long sum2=0;
    	for(auto x:b){
    		sum2 = sum2*base + (unsigned long long)(x-'a'+1);
    	}	
    	
    	int ans=0;
    	unsigned long long qkey = (sum1<<32) ^ (sum2 & 0xffffffffULL);
    	auto it = mp.find(qkey);
    	if(it!=mp.end()){
    		ans += it->second;
    	}
    	for(auto &pr: heavy){
    		int i = pr.first;
    		int sz = pr.second;
    		if((int)a.size() > sz || (int)b.size() > sz) continue;
    		unsigned long long x = get(h[i], 1, (int)a.size());
    		if(sum1!=x) continue;
    		unsigned long long y = get(h[i], sz-(int)b.size()+1, sz);
    		if(sum2!=y) continue;
    		ans++;
    	}
    	cout << ans << '\n';
    }    
    
    return 0;
}

Compilation message (stderr)

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen(".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...