답안 #129089

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129089 2019-07-11T15:09:46 Z mirbek01 Selling RNA Strands (JOI16_selling_rna) C++11
100 / 100
858 ms 162168 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

const int M = 2e6 + 2;

int n, m, ans[N], us[M];
int bor[2][4][M], cnt;
string s[N];
vector < pair <int, int> > qe[M];
vector < int > ft[M];

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

void add(string s, int tp){
      int v = 0;
      for(int i = 0; i < s.size(); i ++){
            int x = get(s[i]);
            if(!bor[tp][x][v])
                  bor[tp][x][v] = ++ cnt;
            v = bor[tp][x][v];
      }
}

int f_v(string s, int tp){
      int v = 0;
      for(int i = 0; i < s.size(); i ++){
            int x = get(s[i]);
            if(!bor[tp][x][v])
                  return 0;
            v = bor[tp][x][v];
      }
      return v;
}

void dfs(int v, unordered_map <int, int> &cnt){
      for(auto x : ft[v])
            cnt[x] ++;
      for(int i = 0; i < 4; i ++){
            int to = bor[0][i][v];
            if(!to)
                  continue;
            unordered_map <int, int> new_cnt;
            dfs(to, new_cnt);
            if((int)cnt.size() < (int)new_cnt.size())
                  swap(cnt, new_cnt);
            for(auto x : new_cnt)
                  cnt[x.first] += x.second;
      }
      for(auto x : qe[v]){
            ans[x.second] += cnt[x.first];
      }
}

int main(){
      scanf("%d %d", &n, &m);

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

      sort(s + 1, s + n + 1);

      for(int i = 1; i <= n; i ++){
            add(s[i], 0);
      }

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

      for(int i = 1; i <= m; i ++){
            string pf, sf;
            cin >> pf >> sf;
            reverse(sf.begin(), sf.end());
            int v1 = f_v(pf, 0), v2 = f_v(sf, 1);
            if(!v1 || !v2)
                  continue;
            qe[v1].emplace_back( make_pair(v2, i) );
            us[v2] = 1;
      }

      for(int i = 1; i <= n; i ++){
            int v1 = 0, v2 = 0;
            reverse(s[i].begin(), s[i].end());
            v1 = f_v(s[i], 0);
            reverse(s[i].begin(), s[i].end());
            for(int j = 0; j < s[i].size(); j ++){
                  int x = get(s[i][j]);
                  v2 = bor[1][x][v2];
                  if(us[v2]){
                        ft[v1].emplace_back(v2);
                  }
            }
      }

      unordered_map<int, int> cnt;
      dfs(0, cnt);

      for(int i = 1; i <= m; i ++){
            printf("%d\n", ans[i]);
      }
}

Compilation message

selling_rna.cpp: In function 'void add(std::__cxx11::string, int)':
selling_rna.cpp:27:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < s.size(); i ++){
                      ~~^~~~~~~~~~
selling_rna.cpp: In function 'int f_v(std::__cxx11::string, int)':
selling_rna.cpp:37:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < s.size(); i ++){
                      ~~^~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:99:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j < s[i].size(); j ++){
                            ~~^~~~~~~~~~~~~
selling_rna.cpp:66:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d", &n, &m);
       ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 97656 KB Output is correct
2 Correct 91 ms 97400 KB Output is correct
3 Correct 90 ms 97400 KB Output is correct
4 Correct 92 ms 97404 KB Output is correct
5 Correct 91 ms 97528 KB Output is correct
6 Correct 91 ms 97504 KB Output is correct
7 Correct 90 ms 97400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 434 ms 132272 KB Output is correct
2 Correct 432 ms 130520 KB Output is correct
3 Correct 774 ms 141676 KB Output is correct
4 Correct 858 ms 141056 KB Output is correct
5 Correct 477 ms 142712 KB Output is correct
6 Correct 477 ms 143248 KB Output is correct
7 Correct 679 ms 162168 KB Output is correct
8 Correct 567 ms 112820 KB Output is correct
9 Correct 551 ms 117964 KB Output is correct
10 Correct 448 ms 117368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 98696 KB Output is correct
2 Correct 141 ms 98420 KB Output is correct
3 Correct 152 ms 98648 KB Output is correct
4 Correct 142 ms 98304 KB Output is correct
5 Correct 142 ms 98552 KB Output is correct
6 Correct 152 ms 98524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 97656 KB Output is correct
2 Correct 91 ms 97400 KB Output is correct
3 Correct 90 ms 97400 KB Output is correct
4 Correct 92 ms 97404 KB Output is correct
5 Correct 91 ms 97528 KB Output is correct
6 Correct 91 ms 97504 KB Output is correct
7 Correct 90 ms 97400 KB Output is correct
8 Correct 434 ms 132272 KB Output is correct
9 Correct 432 ms 130520 KB Output is correct
10 Correct 774 ms 141676 KB Output is correct
11 Correct 858 ms 141056 KB Output is correct
12 Correct 477 ms 142712 KB Output is correct
13 Correct 477 ms 143248 KB Output is correct
14 Correct 679 ms 162168 KB Output is correct
15 Correct 567 ms 112820 KB Output is correct
16 Correct 551 ms 117964 KB Output is correct
17 Correct 448 ms 117368 KB Output is correct
18 Correct 159 ms 98696 KB Output is correct
19 Correct 141 ms 98420 KB Output is correct
20 Correct 152 ms 98648 KB Output is correct
21 Correct 142 ms 98304 KB Output is correct
22 Correct 142 ms 98552 KB Output is correct
23 Correct 152 ms 98524 KB Output is correct
24 Correct 471 ms 126836 KB Output is correct
25 Correct 490 ms 127316 KB Output is correct
26 Correct 441 ms 126412 KB Output is correct
27 Correct 720 ms 135032 KB Output is correct
28 Correct 538 ms 107676 KB Output is correct
29 Correct 427 ms 99704 KB Output is correct