Submission #1040418

# Submission time Handle Problem Language Result Execution time Memory
1040418 2024-08-01T03:46:13 Z shiroihana Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
209 ms 277844 KB
#include <bits/stdc++.h>
using namespace std;

int cl () {
	cerr << "Time used: " << 1.0 * clock() / CLOCKS_PER_SEC << "s ";
	return 0;
}

const int N = (int)1e5;
const char CHAR[4] = {'A', 'C', 'G', 'U'};
int NUM[256];

int n, m;

string s[N + 1];

struct TrieP {
	struct Node {
		Node *child[4];
		int l, r, exist;
		Node () : l(N + 1), r(0), exist(0) {
			for (int t(0); t < 4; ++t) child[t] = NULL;
		}
	};
	
	Node *root;
	TrieP () {
		root = new Node();
	}
	
	void Add (const string &x, int id) {
		Node *p = root;
		for (char f: x) {
			int c = NUM[(int)f];
			if (p -> child[c] == NULL) p -> child[c] = new Node();
			p = p -> child[c];
			p -> l = min(p -> l, id);
			p -> r = max(p -> r, id);
		}
		++(p -> exist);
	}
	
	pair <int, int> getLR (const string &P) {
		Node *p = root;
		for (char f: P) {
			int c = NUM[(int)f];
			if (p -> child[c] == NULL) return make_pair(N + 1, 0);
			p = p -> child[c];
		}
		return make_pair(p -> l, p -> r);
	}

	string curString = "";
	int curI = 0;
	
	void dfs (const Node *p) {
		for (int t(0); t < 4; ++t) if (p -> child[t] != NULL) {
			curString += CHAR[t];
			for (int i(0); i < p -> child[t] -> exist; ++i) {
				s[++curI] = curString;
			}
			dfs(p -> child[t]);
			curString.pop_back();
		}
	}
} tree1;

void init () {
	TrieP list;
	cin >> n >> m; string x;
	for (int i(1); i <= n; ++i) {
		cin >> x;
		list.Add(x, -1);
	}
	list.dfs(list.root);
}

struct TrieQ {
	struct Node {
		Node *child[4];
		vector <int> id;
		Node () {
			for (int t(0); t < 4; ++t) child[t] = NULL;
		}
	};
	
	Node *root;
	TrieQ () {
		root = new Node();
	}
	
	void Add (string x, int i) {
		reverse(x.begin(), x.end());
		Node *p = root;
		for (char f: x) {
			int c = NUM[(int)f];
			if (p -> child[c] == NULL) p -> child[c] = new Node();
			p = p -> child[c];
			(p -> id).push_back(i);
		}
	}
	
	int getCount (string Q, int l, int r) {
		if (l > r) return 0;
		reverse(Q.begin(), Q.end());
		Node *p = root;
		for (char f: Q) {
			int c = NUM[(int)f];
			if (p -> child[c] == NULL) return 0;
			p = p -> child[c];
		}
		int lo = lower_bound((p -> id).begin(), (p -> id).end(), l) - (p -> id).begin();
		int up = upper_bound((p -> id).begin(), (p -> id).end(), r) - (p -> id).begin();
		return up - lo;
	}
} tree2;

void prep () {
	for (int i(1); i <= n; ++i) {
		tree1.Add(s[i], i);
		tree2.Add(s[i], i);
	}
}

int main () {
	#define PHILE "base"
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if (fopen(PHILE".inp", "r")) {
		freopen(PHILE".inp", "r", stdin); freopen(PHILE".out", "w", stdout);
	}
	
	NUM[(int)'A'] = 0;
	NUM[(int)'C'] = 1;
	NUM[(int)'G'] = 2;
	NUM[(int)'U'] = 3;

	init();
	prep();
	
	string P, Q;
	while (m--) {
		cin >> P >> Q;
		pair <int, int> LR = tree1.getLR(P);
		cout << tree2.getCount(Q, LR.first, LR.second) << '\n';
	}

	return cl();
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:129:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |   freopen(PHILE".inp", "r", stdin); freopen(PHILE".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:129:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |   freopen(PHILE".inp", "r", stdin); freopen(PHILE".out", "w", stdout);
      |                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Correct 1 ms 3588 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 1 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 190928 KB Output is correct
2 Correct 169 ms 185172 KB Output is correct
3 Correct 181 ms 263364 KB Output is correct
4 Correct 205 ms 251212 KB Output is correct
5 Correct 196 ms 273888 KB Output is correct
6 Correct 209 ms 277844 KB Output is correct
7 Correct 49 ms 19540 KB Output is correct
8 Correct 144 ms 173908 KB Output is correct
9 Correct 127 ms 147540 KB Output is correct
10 Correct 108 ms 142316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 4312 KB Output is correct
2 Correct 9 ms 4956 KB Output is correct
3 Correct 13 ms 4956 KB Output is correct
4 Correct 8 ms 4444 KB Output is correct
5 Correct 12 ms 4956 KB Output is correct
6 Correct 11 ms 4908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Correct 1 ms 3588 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 1 ms 3420 KB Output is correct
8 Correct 143 ms 190928 KB Output is correct
9 Correct 169 ms 185172 KB Output is correct
10 Correct 181 ms 263364 KB Output is correct
11 Correct 205 ms 251212 KB Output is correct
12 Correct 196 ms 273888 KB Output is correct
13 Correct 209 ms 277844 KB Output is correct
14 Correct 49 ms 19540 KB Output is correct
15 Correct 144 ms 173908 KB Output is correct
16 Correct 127 ms 147540 KB Output is correct
17 Correct 108 ms 142316 KB Output is correct
18 Correct 12 ms 4312 KB Output is correct
19 Correct 9 ms 4956 KB Output is correct
20 Correct 13 ms 4956 KB Output is correct
21 Correct 8 ms 4444 KB Output is correct
22 Correct 12 ms 4956 KB Output is correct
23 Correct 11 ms 4908 KB Output is correct
24 Correct 130 ms 161392 KB Output is correct
25 Correct 131 ms 161536 KB Output is correct
26 Correct 134 ms 159420 KB Output is correct
27 Correct 148 ms 217508 KB Output is correct
28 Correct 98 ms 38992 KB Output is correct
29 Correct 34 ms 12016 KB Output is correct