답안 #33396

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
33396 2017-10-25T09:55:36 Z aome Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
753 ms 746524 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;

typedef pair<int, int> ii;

int n, m;
string s[N];
vector<ii> go;

int to(char ch) {
	if (ch == 'A') return 0;
	if (ch == 'G') return 1;
	if (ch == 'C') return 2;
	if (ch == 'U') return 3;
}

struct Trie {
	struct Node {
		int nxt[4], cnt;

		Node () {
			memset(nxt, 0, sizeof nxt); cnt = 0;
		}
	};

	int TIME;
	vector<Node> tree;
	vector<int> st, ed;

	Trie() {
		tree.push_back(Node());
	}

	void update(string &s) {
		int cur = 0;
		for (int i = 0; i < s.size(); ++i) {
			int tmp = to(s[i]);
			if (!tree[cur].nxt[tmp]) {
				tree[cur].nxt[tmp] = tree.size(), tree.push_back(Node());
			}
			cur = tree[cur].nxt[tmp], tree[cur].cnt++;
		}
	}

	int query(string &s) {
		int cur = 0;
		for (int i = 0; i < s.size(); ++i) {
			int tmp = to(s[i]);
			if (!tree[cur].nxt[tmp]) return 0;
			cur = tree[cur].nxt[tmp];
		}
		return tree[cur].cnt;
	}

	void dfs(int u) {
		st[u] = ++TIME;
		for (int i = 0; i < 4; ++i) {
			if (tree[u].nxt[i]) dfs(tree[u].nxt[i]); 
		}
		ed[u] = TIME;
	}

	void cal() {
		TIME = 0, st.assign(tree.size(), 0), ed.assign(tree.size(), 0), dfs(0);
	}

	int findU(string &s) {
		int cur = 0;
		for (int i = 0; i < s.size(); ++i) {
			int tmp = to(s[i]);
			cur = tree[cur].nxt[tmp];
		}
		return st[cur];
	}

	ii findQ(string &s) {
		int cur = 0;
		for (int i = 0; i < s.size(); ++i) {
			int tmp = to(s[i]);
			if (!tree[cur].nxt[tmp]) return ii(0, 0);
			cur = tree[cur].nxt[tmp];
		}
		return ii(st[cur], ed[cur]);
	} 

} it[4 * N], pref;

void build(int i, int l, int r) {
	for (int j = l; j <= r; ++j) it[i].update(s[go[j].second]);
	if (l == r) return;	
	int mid = (l + r) >> 1;
	build(i << 1, l, mid), build(i << 1 | 1, mid + 1, r);
}

int query(int i, int l, int r, int u, int v, string &s) {
	if (l > v || u > r) return 0;
	if (u <= l && r <= v) return it[i].query(s);
	int mid = (l + r) >> 1;
	return query(i << 1, l, mid, u, v, s) + query(i << 1 | 1, mid + 1, r, u, v, s);
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n; ++i) {
		cin >> s[i], pref.update(s[i]);
	}
	pref.cal();
	for (int i = 0; i < n; ++i) {
		int tmp = pref.findU(s[i]);
		go.push_back(ii(tmp, i));
	}
	sort(go.begin(), go.end());
	for (int i = 0; i < n; ++i) {
		reverse(s[i].begin(), s[i].end());
	}
	build(1, 0, n - 1);
	for (int i = 0; i < m; ++i) {
		string p, q; cin >> p >> q;
		ii tmp = pref.findQ(p);
		if (!tmp.first) {
			printf("0\n"); continue;
		}
		int l = lower_bound(go.begin(), go.end(), ii(tmp.first, 0)) - go.begin();
		int r = lower_bound(go.begin(), go.end(), ii(tmp.second + 1, 0)) - go.begin() - 1;
		reverse(q.begin(), q.end());
		//cout << query(1, 0, n - 1, l, r, q) << '\n';
		printf("%d\n", query(1, 0, n - 1, l, r, q));
	}
}

Compilation message

selling_rna.cpp: In member function 'void Trie::update(std::__cxx11::string&)':
selling_rna.cpp:38:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < s.size(); ++i) {
                     ^
selling_rna.cpp: In member function 'int Trie::query(std::__cxx11::string&)':
selling_rna.cpp:49:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < s.size(); ++i) {
                     ^
selling_rna.cpp: In member function 'int Trie::findU(std::__cxx11::string&)':
selling_rna.cpp:71:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < s.size(); ++i) {
                     ^
selling_rna.cpp: In member function 'ii Trie::findQ(std::__cxx11::string&)':
selling_rna.cpp:80:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < s.size(); ++i) {
                     ^
selling_rna.cpp: In function 'int to(char)':
selling_rna.cpp:17:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 49104 KB Output is correct
2 Correct 26 ms 49104 KB Output is correct
3 Correct 29 ms 49104 KB Output is correct
4 Correct 23 ms 49104 KB Output is correct
5 Correct 23 ms 49104 KB Output is correct
6 Correct 26 ms 49104 KB Output is correct
7 Correct 33 ms 49104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 499 ms 705924 KB Output is correct
2 Correct 509 ms 746524 KB Output is correct
3 Correct 509 ms 288128 KB Output is correct
4 Correct 453 ms 278660 KB Output is correct
5 Correct 653 ms 619524 KB Output is correct
6 Correct 619 ms 622128 KB Output is correct
7 Correct 216 ms 134768 KB Output is correct
8 Correct 456 ms 543836 KB Output is correct
9 Correct 419 ms 490332 KB Output is correct
10 Correct 399 ms 398732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 56736 KB Output is correct
2 Correct 89 ms 56796 KB Output is correct
3 Correct 113 ms 56092 KB Output is correct
4 Correct 79 ms 53700 KB Output is correct
5 Correct 79 ms 55468 KB Output is correct
6 Correct 113 ms 55444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 49104 KB Output is correct
2 Correct 26 ms 49104 KB Output is correct
3 Correct 29 ms 49104 KB Output is correct
4 Correct 23 ms 49104 KB Output is correct
5 Correct 23 ms 49104 KB Output is correct
6 Correct 26 ms 49104 KB Output is correct
7 Correct 33 ms 49104 KB Output is correct
8 Correct 499 ms 705924 KB Output is correct
9 Correct 509 ms 746524 KB Output is correct
10 Correct 509 ms 288128 KB Output is correct
11 Correct 453 ms 278660 KB Output is correct
12 Correct 653 ms 619524 KB Output is correct
13 Correct 619 ms 622128 KB Output is correct
14 Correct 216 ms 134768 KB Output is correct
15 Correct 456 ms 543836 KB Output is correct
16 Correct 419 ms 490332 KB Output is correct
17 Correct 399 ms 398732 KB Output is correct
18 Correct 109 ms 56736 KB Output is correct
19 Correct 89 ms 56796 KB Output is correct
20 Correct 113 ms 56092 KB Output is correct
21 Correct 79 ms 53700 KB Output is correct
22 Correct 79 ms 55468 KB Output is correct
23 Correct 113 ms 55444 KB Output is correct
24 Correct 519 ms 713904 KB Output is correct
25 Correct 596 ms 718252 KB Output is correct
26 Correct 523 ms 706612 KB Output is correct
27 Correct 466 ms 272444 KB Output is correct
28 Correct 753 ms 262932 KB Output is correct
29 Correct 479 ms 111776 KB Output is correct