# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
44689 | choikiwon | Selling RNA Strands (JOI16_selling_rna) | C++17 | 1445 ms | 313804 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int f(char c) {
if(c == 'A') return 0;
if(c == 'G') return 1;
if(c == 'C') return 2;
if(c == 'U') return 3;
if(c == '#') return 4;
if(c == '@') return 5;
}
vector<int> adj[5000010];
int cnt[5000010], pnt[100010];
int cc[5000010];
int dp(int u) {
int &ret = cc[u];
if(ret != -1) return ret;
ret = cnt[u];
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
ret += dp(v);
}
return ret;
}
struct aho_corasick {
vector<vector<int> > trie;
vector<int> fail;
int n;
void init(vector<string> &V) {
trie.push_back(vector<int>(6, -1));
fail.push_back(0);
n = V.size();
for(int i = 0; i < V.size(); i++) {
int u = 0;
for(int j = 0; j < V[i].size(); j++) {
if(trie[u][ f(V[i][j]) ] == -1) {
trie[u][ f(V[i][j]) ] = trie.size();
trie.push_back(vector<int>(6, -1));
fail.push_back(0);
}
u = trie[u][ f(V[i][j]) ];
}
pnt[i] = u;
}
queue<int> q;
for(int i = 0; i < 6; i++) {
if(trie[0][i] != -1) q.push(trie[0][i]);
}
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = 0; i < 6; i++) {
int v = trie[u][i];
if(v != -1) {
int p = fail[u];
while(p && trie[p][i] == -1) p = fail[p];
fail[v] = trie[p][i] == -1? 0 : trie[p][i];
if(v) adj[ fail[v] ].push_back(v);
q.push(v);
}
}
}
}
void query(string &S) {
int u = 0;
for(int i = 0; i < S.size(); i++) {
while(u && trie[u][ f(S[i]) ] == -1) u = fail[u];
if(trie[u][ f(S[i]) ] != -1) u = trie[u][ f(S[i]) ];
cnt[u]++;
}
memset(cc, -1, sizeof(cc));
for(int i = 0; i < n; i++) {
printf("%d\n", dp(pnt[i]));
}
}
} aho;
int N, M;
string S;
vector<string> V;
void getStr(string &s) {
while(1) {
char c = getchar();
if(c == ' ' || c == '\n') break;
s.push_back(c);
}
}
int main() {
scanf("%d %d", &N, &M);
getchar();
for(int i = 0; i < N; i++) {
string s; getStr(s);
S += s;
S += '#';
S += s;
S += '@';
}
for(int i = 0; i < M; i++) {
string p, q;
getStr(p);
getStr(q);
V.push_back(q + '#' + p);
}
aho.init(V);
aho.query(S);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |