Submission #332614

# Submission time Handle Problem Language Result Execution time Memory
332614 2020-12-03T01:33:39 Z Kevin_Zhang_TW Selling RNA Strands (JOI16_selling_rna) C++17
10 / 100
683 ms 1048580 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T>
bool chmax(T &v, T val) { return v < val ? (v = val, true) : false; }
template<class T>
bool chmin(T &v, T val) { return val < v ? (v = val, true) : false; }

#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = "), kout(args)
void kout() { cerr << endl; }
template<class T, class ...U>
void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 100010, MAX_M = 5000;

int n, m, enc[256];
string w[MAX_N], p[MAX_N], q[MAX_N];
struct trie {
	trie* nxt[4]{};
	bitset<MAX_M> ok;
	void rev_insert(const string &w, int id) {
		trie* now = this;
		for (int i = (int)w.size() - 1;i >= 0;--i) {
			auto &pt = now->nxt[ enc[ w[i] ] ];
			if (!pt) pt = new trie;
			now = pt;
			pt->ok[ id ] = true;
		}
	}
	bitset<MAX_M> rev_qry(const string &w) {
		trie *now = this;
		for (int i = (int)w.size() - 1;i >= 0;--i) {
			auto &pt = now->nxt[ enc[ w[i] ] ];
			if (!pt) return bitset<MAX_M>() ;
			now = pt;
		}
		return now->ok;
	}
	void insert(const string &w, int id) {
		trie* now = this;
		for (int i = 0;i < w.size();++i) {
			auto &pt = now->nxt[ enc[ w[i] ] ];
			if (!pt) pt = new trie;
			now = pt;
			pt->ok[ id ] = true;
		}
	}
	bitset<MAX_M> qry(const string &w) {
		trie *now = this;
		for (int i = 0;i < w.size();++i) {
			auto &pt = now->nxt[ enc[ w[i] ] ];
			if (!pt) return bitset<MAX_M>() ;
			now = pt;
		}
		return now->ok;
	}
}ac, ca;

void solve() {
	for (int i = 0;i < n;++i) {
		ac.insert(w[i], i);
		ca.rev_insert(w[i], i);
	}
	for (int i = 0;i < m;++i) {
		cout << ( ac.qry(p[i]) & ca.rev_qry(q[i]) ).count() << '\n';
	}
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	enc['A'] = 0, enc['G'] = 1, enc['C'] = 2, enc['U'] = 3;
	cin >> n >> m;
	for (int i = 0;i < n;++i)
		cin >> w[i];
	for (int i = 0;i < m;++i)
		cin >> p[i] >> q[i];
	
	if (n > 5000) return 1;

	solve();
	return 0;

	for (int i = 0;i < m;++i) {
		int res = 0;
		for (int j = 0;j < n;++j) {
			if (max(p[i].size(), q[i].size()) > w[j].size()) continue;
			res += p[i] == w[j].substr(0, p[i].size()) &&
			   	q[i] == w[j].substr(w[j].size() - q[i].size());
		}
		cout << res << '\n';
	}
}

Compilation message

selling_rna.cpp: In member function 'void trie::rev_insert(const string&, int)':
selling_rna.cpp:33:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   33 |    auto &pt = now->nxt[ enc[ w[i] ] ];
      |                                   ^
selling_rna.cpp: In member function 'std::bitset<5000> trie::rev_qry(const string&)':
selling_rna.cpp:42:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   42 |    auto &pt = now->nxt[ enc[ w[i] ] ];
      |                                   ^
selling_rna.cpp: In member function 'void trie::insert(const string&, int)':
selling_rna.cpp:50:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |   for (int i = 0;i < w.size();++i) {
      |                  ~~^~~~~~~~~~
selling_rna.cpp:51:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   51 |    auto &pt = now->nxt[ enc[ w[i] ] ];
      |                                   ^
selling_rna.cpp: In member function 'std::bitset<5000> trie::qry(const string&)':
selling_rna.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int i = 0;i < w.size();++i) {
      |                  ~~^~~~~~~~~~
selling_rna.cpp:60:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   60 |    auto &pt = now->nxt[ enc[ w[i] ] ];
      |                                   ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 683 ms 1048580 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 10240 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Runtime error 683 ms 1048580 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Halted 0 ms 0 KB -