Submission #522840

# Submission time Handle Problem Language Result Execution time Memory
522840 2022-02-06T02:11:10 Z thiago_bastos Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
1032 ms 190872 KB
#include "bits/stdc++.h"
 
using namespace std;
 
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
 
const int N = 2e6, mod = 1e9 + 9, base = 1845;
 
vector<int> h(string& s) {
	int n = s.size();
	vector<int> a(n + 1);
	i64 k = 1;
	a[0] = 0;
	for(int i = 1; i <= n; ++i) {
		int x0 = a[i - 1];
		int& x1 = a[i];
		k = k * base % mod;
		x1 = (x0 + (s[i - 1] - '0' + 1) * k) % mod;
	}
	a.erase(a.begin());
	return a;
}
 
int t[N][4], nos = 1;
vector<ii> query[N];
unordered_map<int, int> cnt;
vector<int> ans, H[N];
 
void push(string& s) {
	int i = 0;
	for(char ch : s) {
		int& k = t[i][ch - '0'];
		if(k < 0) {
			memset(t[nos], -1, sizeof(t[nos]));
			k = nos++;
		}
		i = k;
	}
	reverse(s.begin(), s.end());
	for(int y : h(s)) H[i].push_back(y);
}
 
void f(string& s) {
	string alpha = "AGCU";
	for(char& ch : s) {
		for(int i = 0; i < 4; ++i) {
			if(ch == alpha[i]) {
				ch = i + '0';
				break;
			}
		}
	}
}
 
void dfs(int u) {	
	for(auto [pos, ht] : query[u]) {
		auto it = cnt.find(ht);
		if(it == cnt.end()) continue;
		ans[pos] -= it->second;
	}

	for(int v = 0; v < 4; ++v) {
		int z = t[u][v];
		if(z < 0) continue;
		dfs(z);
	}

	for(int ht : H[u]) ++cnt[ht];

	for(auto [pos, ht] : query[u]) {
		auto it = cnt.find(ht);
		if(it == cnt.end()) continue;
		ans[pos] += it->second;
	}
}
 
void solve() {
	
	int n, m;
 
	cin >> n >> m;
 
	memset(t[0], -1, sizeof(t[0]));
	ans.assign(m, 0);
 
	for(int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		f(s);
		push(s);
	}
 
	for(int i = 0; i < m; ++i) {
		string a, b;
		int k = 0;
		cin >> a >> b;
		f(a);
		f(b);
		reverse(b.begin(), b.end());
		for(char ch : a) {
			if(t[k][ch - '0'] < 0) {
				k = -1;
				break;
			}
			k = t[k][ch - '0'];
		}
		if(k < 0) continue;
		query[k].emplace_back(i, h(b).back());
	}
 
	dfs(0);
 
	for(int k : ans) cout << k << '\n';
}

int main() {
	int t = 1;
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	// cin >> t;
	while(t--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94220 KB Output is correct
2 Correct 45 ms 94236 KB Output is correct
3 Correct 48 ms 94216 KB Output is correct
4 Correct 44 ms 94236 KB Output is correct
5 Correct 45 ms 94252 KB Output is correct
6 Correct 49 ms 94216 KB Output is correct
7 Correct 44 ms 94204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 934 ms 190872 KB Output is correct
2 Correct 899 ms 187368 KB Output is correct
3 Correct 245 ms 136396 KB Output is correct
4 Correct 218 ms 135016 KB Output is correct
5 Correct 617 ms 169700 KB Output is correct
6 Correct 642 ms 170552 KB Output is correct
7 Correct 131 ms 104388 KB Output is correct
8 Correct 448 ms 143568 KB Output is correct
9 Correct 420 ms 138280 KB Output is correct
10 Correct 353 ms 137800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 95652 KB Output is correct
2 Correct 61 ms 95484 KB Output is correct
3 Correct 62 ms 95688 KB Output is correct
4 Correct 58 ms 95368 KB Output is correct
5 Correct 62 ms 95420 KB Output is correct
6 Correct 64 ms 95556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 94220 KB Output is correct
2 Correct 45 ms 94236 KB Output is correct
3 Correct 48 ms 94216 KB Output is correct
4 Correct 44 ms 94236 KB Output is correct
5 Correct 45 ms 94252 KB Output is correct
6 Correct 49 ms 94216 KB Output is correct
7 Correct 44 ms 94204 KB Output is correct
8 Correct 934 ms 190872 KB Output is correct
9 Correct 899 ms 187368 KB Output is correct
10 Correct 245 ms 136396 KB Output is correct
11 Correct 218 ms 135016 KB Output is correct
12 Correct 617 ms 169700 KB Output is correct
13 Correct 642 ms 170552 KB Output is correct
14 Correct 131 ms 104388 KB Output is correct
15 Correct 448 ms 143568 KB Output is correct
16 Correct 420 ms 138280 KB Output is correct
17 Correct 353 ms 137800 KB Output is correct
18 Correct 64 ms 95652 KB Output is correct
19 Correct 61 ms 95484 KB Output is correct
20 Correct 62 ms 95688 KB Output is correct
21 Correct 58 ms 95368 KB Output is correct
22 Correct 62 ms 95420 KB Output is correct
23 Correct 64 ms 95556 KB Output is correct
24 Correct 1002 ms 185804 KB Output is correct
25 Correct 1032 ms 186476 KB Output is correct
26 Correct 1000 ms 185828 KB Output is correct
27 Correct 211 ms 131148 KB Output is correct
28 Correct 224 ms 116268 KB Output is correct
29 Correct 140 ms 101316 KB Output is correct