Submission #112379

# Submission time Handle Problem Language Result Execution time Memory
112379 2019-05-19T07:18:28 Z Mahdi_Jfri Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
298 ms 75788 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back

const int maxn = 1e5 + 20;
const int maxm = 2e6 + 20;
const int maxs = 5;

vector<pair<int , int> > query[maxn];

string s[maxn] , a[maxn] , b[maxn];

int nxt[maxm][maxs] , mx[maxm] , sz[maxm] , id = 1 , res[maxn];

void add(int ind)
{
	int p = 0;
	for(auto ch : s[ind])
	{
		if(!nxt[p][ch])
			nxt[p][ch] = id++;

		mx[p] = ind;
		sz[p]++;
		p = nxt[p][ch];
	}

	mx[p] = ind;
	sz[p]++;
}

pair<int , int> fn(string s)
{
	int p = 0;
	for(auto ch : s)
	{
		if(!nxt[p][ch])
			return make_pair(0 , 0);

		p = nxt[p][ch];
	}

	return {mx[p] , sz[p]};
}

void reval(string &s)
{
	for(auto &ch : s)
	{
		if(ch == 'A')
			ch = 0;
		else if(ch == 'G')
			ch = 1;
		else if(ch == 'C')
			ch = 2;
		else
			ch = 3;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n , q;
	cin >> n >> q;

	for(int i = 1; i <= n; i++)
		cin >> s[i] , reval(s[i]);
	sort(s + 1 , s + n + 1);

	for(int i = 1; i <= n; i++)
		add(i);

	assert(sz[0] == n);

	for(int i = 0; i < q; i++)
	{
		cin >> a[i] >> b[i];
		reval(a[i]) , reval(b[i]);
		reverse(b[i].begin() , b[i].end());

		auto tmp = fn(a[i]);
		int r = tmp.first , l = tmp.first - tmp.second;
		query[r].pb({i , 1});
		query[l].pb({i ,-1});
	}

	memset(nxt , 0 , sizeof nxt);
	memset(mx , 0 , sizeof mx);
	memset(sz , 0 , sizeof sz);
	id = 1;

	for(int i = 1; i <= n; i++)
	{
		reverse(s[i].begin() , s[i].end());
		add(i);
		assert(sz[0] == i);

		for(auto tmp : query[i])
			res[tmp.first] += tmp.second * fn(b[tmp.first]).second;
	}

	for(int i = 0; i < q; i++)
		cout << res[i] << endl;
}









Compilation message

selling_rna.cpp: In function 'void add(int)':
selling_rna.cpp:22:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   if(!nxt[p][ch])
                ^
selling_rna.cpp:23:13: warning: array subscript has type 'char' [-Wchar-subscripts]
    nxt[p][ch] = id++;
             ^
selling_rna.cpp:27:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   p = nxt[p][ch];
                ^
selling_rna.cpp: In function 'std::pair<int, int> fn(std::__cxx11::string)':
selling_rna.cpp:39:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   if(!nxt[p][ch])
                ^
selling_rna.cpp:42:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   p = nxt[p][ch];
                ^
# Verdict Execution time Memory Grader output
1 Correct 52 ms 66936 KB Output is correct
2 Correct 52 ms 66936 KB Output is correct
3 Correct 48 ms 66936 KB Output is correct
4 Correct 51 ms 67064 KB Output is correct
5 Correct 52 ms 66936 KB Output is correct
6 Correct 51 ms 66808 KB Output is correct
7 Correct 50 ms 66908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 71196 KB Output is correct
2 Correct 108 ms 71544 KB Output is correct
3 Correct 121 ms 71508 KB Output is correct
4 Correct 119 ms 71552 KB Output is correct
5 Correct 134 ms 70224 KB Output is correct
6 Correct 126 ms 70248 KB Output is correct
7 Correct 120 ms 73084 KB Output is correct
8 Correct 153 ms 73596 KB Output is correct
9 Correct 126 ms 73720 KB Output is correct
10 Correct 103 ms 71192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 68500 KB Output is correct
2 Correct 107 ms 67980 KB Output is correct
3 Correct 120 ms 68564 KB Output is correct
4 Correct 116 ms 68216 KB Output is correct
5 Correct 106 ms 68088 KB Output is correct
6 Correct 151 ms 68472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 66936 KB Output is correct
2 Correct 52 ms 66936 KB Output is correct
3 Correct 48 ms 66936 KB Output is correct
4 Correct 51 ms 67064 KB Output is correct
5 Correct 52 ms 66936 KB Output is correct
6 Correct 51 ms 66808 KB Output is correct
7 Correct 50 ms 66908 KB Output is correct
8 Correct 98 ms 71196 KB Output is correct
9 Correct 108 ms 71544 KB Output is correct
10 Correct 121 ms 71508 KB Output is correct
11 Correct 119 ms 71552 KB Output is correct
12 Correct 134 ms 70224 KB Output is correct
13 Correct 126 ms 70248 KB Output is correct
14 Correct 120 ms 73084 KB Output is correct
15 Correct 153 ms 73596 KB Output is correct
16 Correct 126 ms 73720 KB Output is correct
17 Correct 103 ms 71192 KB Output is correct
18 Correct 137 ms 68500 KB Output is correct
19 Correct 107 ms 67980 KB Output is correct
20 Correct 120 ms 68564 KB Output is correct
21 Correct 116 ms 68216 KB Output is correct
22 Correct 106 ms 68088 KB Output is correct
23 Correct 151 ms 68472 KB Output is correct
24 Correct 138 ms 72284 KB Output is correct
25 Correct 191 ms 73436 KB Output is correct
26 Correct 125 ms 71912 KB Output is correct
27 Correct 180 ms 72184 KB Output is correct
28 Correct 298 ms 75788 KB Output is correct
29 Correct 246 ms 70136 KB Output is correct