Submission #359347

# Submission time Handle Problem Language Result Execution time Memory
359347 2021-01-26T22:49:51 Z aZvezda Selling RNA Strands (JOI16_selling_rna) C++14
0 / 100
1352 ms 1048580 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 1e6 + 10;
struct Node {
	int mp[4];
	int cnt;
	Node() {
		for(int i = 0; i < 4; i ++) {
			mp[i] = -1;
		}
		cnt = 0;
	}
};

struct Trie {
	vector<Node> nds;
	vector<string> strs;
	Trie() {
		nds.resize(1);
	}
	void add(const string &str) {
	    strs.push_back(str);
		int curr = 0;
		nds[curr].cnt ++;
		for(int i = 0; i < str.size(); i ++) {
			if(nds[curr].mp[str[i]] == -1) {
				nds[curr].mp[str[i]] = nds.size();
				nds.push_back(Node());
			}
			curr = nds[curr].mp[str[i]];
			nds[curr].cnt ++;
		}
	}
	int size() {return nds.size();}
	int cnt(const string &str) {
		int curr = 0;
		for(int i = 0; i < str.size(); i ++) {
			curr = nds[i].mp[str[i]];
			if(curr == -1) {return 0;}
		}
		return nds[curr].cnt;
	}
	void outputtrie(int x, vector<int> out) {
		cout << x << " " << nds[x].cnt << " ";
		for(auto it : out) {
			cout << it;
		}
		cout << endl;
		for(int i = 0; i < 4; i ++) {
			if(nds[x].mp[i] == -1) {continue;}
			out.push_back(i);
			outputtrie(nds[x].mp[i], out);
			out.pop_back();
		}
	}
};

vector<int> fin[MAX_N], quer[MAX_N];
int mp[MAX_N][4];
int n, q, ans[MAX_N], cnt = 1;
string text[MAX_N];
string in[MAX_N][2];

void buildTrie() {
	for(int i = 0; i < n; i ++) {
		int curr = 0;
		for(int j = 0; j < text[i].size(); j ++) {
			if(mp[curr][text[i][j]] == -1) {
				mp[curr][text[i][j]] = cnt;
				cnt ++;
			}
			curr = mp[curr][text[i][j]];
		}
		fin[curr].push_back(i);
	}
}

void transform(string &str) {
	for(auto &it : str) {
		if(it == 'A') {
			it = 0;
		} else if(it == 'U') {
			it = 1;
		} else if(it == 'G') {
			it = 2;
		} else if(it == 'C') {
			it = 3;
		}
	}
}

Trie trie[MAX_N];
int who[MAX_N];

void merge(int x, int y) {
	if(trie[who[x]].size() > trie[who[y]].size()) {
		swap(who[x], who[y]);
	}
	for(auto &it : trie[who[y]].strs) {
        trie[who[x]].add(it);
	}
	trie[who[y]].nds.resize(0);
	trie[who[y]].strs.resize(0);
}

void dfs(int x) {
	for(auto it : fin[x]) {
		reverse(text[it].begin(), text[it].end());
		trie[who[x]].add(text[it]);
	}
	for(int i = 0; i < 4; i ++) {
		if(mp[x][i] == -1) {continue;}
		dfs(mp[x][i]);
		merge(x, mp[x][i]);
	}
	for(auto it : quer[x]) {
		ans[it] = trie[who[x]].cnt(in[it][1]);
	}
	return;
	cout << endl;
	trie[who[x]].outputtrie(0, {});
	cout << endl << endl;
}

void computeAns() {
	for(int i = 0; i < q; i ++) {
		int curr = 0;
		for(int j = 0; j < in[i][0].size(); j ++) {
			curr = mp[curr][in[i][0][j]];
			if(curr == -1) {
				break;
			}
		}
		if(curr == -1) {continue;}
		quer[curr].push_back(i);
	}
	dfs(0);
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	for(int i = 0; i < MAX_N; i ++) {
		for(int j = 0; j < 4; j ++) {
			mp[i][j] = -1;
		}
		who[i] = i;
	}
	cin >> n >> q;
	for(int i = 0; i < n; i ++) {
		cin >> text[i];
		transform(text[i]);
	}
	buildTrie();
	for(int i = 0; i < q; i ++) {
		cin >> in[i][0] >> in[i][1];
		transform(in[i][0]);
		reverse(in[i][1].begin(), in[i][1].end());
		transform(in[i][1]);
	}
	computeAns();
	for(int i = 0; i < q; i ++) {
		cout << ans[i] << endl;
	}
	return 0;
}

Compilation message

selling_rna.cpp: In member function 'void Trie::add(const string&)':
selling_rna.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |   for(int i = 0; i < str.size(); i ++) {
      |                  ~~^~~~~~~~~~~~
selling_rna.cpp:37:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   37 |    if(nds[curr].mp[str[i]] == -1) {
      |                          ^
selling_rna.cpp:38:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   38 |     nds[curr].mp[str[i]] = nds.size();
      |                        ^
selling_rna.cpp:41:30: warning: array subscript has type 'char' [-Wchar-subscripts]
   41 |    curr = nds[curr].mp[str[i]];
      |                              ^
selling_rna.cpp: In member function 'int Trie::cnt(const string&)':
selling_rna.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for(int i = 0; i < str.size(); i ++) {
      |                  ~~^~~~~~~~~~~~
selling_rna.cpp:49:27: warning: array subscript has type 'char' [-Wchar-subscripts]
   49 |    curr = nds[i].mp[str[i]];
      |                           ^
selling_rna.cpp: In function 'void buildTrie()':
selling_rna.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for(int j = 0; j < text[i].size(); j ++) {
      |                  ~~^~~~~~~~~~~~~~~~
selling_rna.cpp:79:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   79 |    if(mp[curr][text[i][j]] == -1) {
      |                          ^
selling_rna.cpp:80:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   80 |     mp[curr][text[i][j]] = cnt;
      |                        ^
selling_rna.cpp:83:30: warning: array subscript has type 'char' [-Wchar-subscripts]
   83 |    curr = mp[curr][text[i][j]];
      |                              ^
selling_rna.cpp: In function 'void computeAns()':
selling_rna.cpp:139:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |   for(int j = 0; j < in[i][0].size(); j ++) {
      |                  ~~^~~~~~~~~~~~~~~~~
selling_rna.cpp:140:31: warning: array subscript has type 'char' [-Wchar-subscripts]
  140 |    curr = mp[curr][in[i][0][j]];
      |                               ^
# Verdict Execution time Memory Grader output
1 Incorrect 197 ms 239212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1352 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 240 ms 246360 KB Output is correct
2 Incorrect 258 ms 253896 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 197 ms 239212 KB Output isn't correct
2 Halted 0 ms 0 KB -