답안 #1106293

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106293 2024-10-29T18:01:55 Z VinhLuu Selling RNA Strands (JOI16_selling_rna) C++17
0 / 100
517 ms 234568 KB
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define all(lpv) lpv.begin(), lpv.end()
#define fi first
#define se second

using namespace std;

typedef pair<int,int> pii;
const int N = 1e5 + 5;
//const int M = 2e6 + 5;
const int oo = 1e9;

int n, m;
string s[N], a[N], b[N];

char change(char c){
  if(c == 'A') return '1';
  if(c == 'C') return '2';
  if(c == 'G') return '3';
  return '4';
}

struct trie{
  trie* child[6];
  vector<int> w;
  trie(){
    for(int i = 0; i <= 5; i ++) child[i] = NULL;
    w.clear();
  }
};

trie* root;

void add(string &x,int id){
  trie* p = root;
  for(auto i : x){
    int val = (i - '0');
    if(p -> child[val] == NULL){
      p -> child[val] = new trie();
    }
    p = p -> child[val];
    p -> w.push_back(id);
  }
}

int query(string &x,int l,int r){
  trie* p = root;
  for(auto i : x){
    int val = (i - '0');
    if(p -> child[val] == NULL) return 0;
    p = p -> child[val];
  }

  auto v = upper_bound(p -> w.begin(), p -> w.end(), r);
  auto u = lower_bound(p -> w.begin(), p -> w.end(), l);
  if(v == p -> w.begin()) return 0;
  if(u == p -> w.end()) return 0;
  v--;
  if((*u) <= (*v)) return (*v) - (*u) + 1;
  return 0;
}


mt19937 rd(15253524352);
int ra(int l,int r){
  return l + rd() % (r - l + 1);
}
const ll mod[2] = {ra(1e9, 2e9), (int)1e9 + 7};
const ll base = 137;

map<ll,int> mx, mi;
int le[N], ri[N];

signed main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }

  root = new trie();

  cin >> n >> m;
  for(int i = 1; i <= n; i ++){
    cin >> s[i];
    for(int j = 0; j < s[i].size(); j ++){
      s[i][j] = change(s[i][j]);
    }
  }
  sort(s + 1, s + n + 1);

//  for(int i = 1; i <= n; i ++) cerr << s[i] << "\n";
//

  for(int i = 1; i <= n; i ++){
    ll h[2];
    h[0] = h[1] = 0;
    for(int j : s[i]){
      for(int k = 0; k <= 1; k ++){
        h[k] = (1ll * h[k] * base % mod[k] + (ll)(j - '0')) % mod[k];
      }
      ll H = 1ll * h[0] * mod[1] + h[1];
      mx[H] = i;
      if(mi[H] == 0) mi[H] = i;
    }
    string hk = s[i];
    reverse(hk.begin(), hk.end());
//    cerr << i << " j\n";
//    for(auto j : s[i]) cerr
    add(hk, i);
  }

  for(int i = 1; i <= m; i ++){
    cin >> a[i] >> b[i];
    for(int j = 0; j < a[i].size(); j ++) a[i][j] = change(a[i][j]);
    for(int j = 0; j < b[i].size(); j ++) b[i][j] = change(b[i][j]);
    ll h[2];
    h[0] = h[1] = 0;
    for(int j : a[i]){
      for(int k = 0; k <= 1; k ++){
        h[k] = (h[k] * base % mod[k] + (int)(j - '0')) % mod[k];
      }
    }
    ll H = 1ll * h[0] * mod[1] + h[1];
    le[i] = mi[H], ri[i] = mx[H];
    if(!le[i]){
      cout << 0 << "\n";
      continue;
    }
    reverse(b[i].begin(), b[i].end());
    cout << query(b[i], le[i], ri[i]) << "\n";
  }


}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for(int j = 0; j < s[i].size(); j ++){
      |                    ~~^~~~~~~~~~~~~
selling_rna.cpp:118:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for(int j = 0; j < a[i].size(); j ++) a[i][j] = change(a[i][j]);
      |                    ~~^~~~~~~~~~~~~
selling_rna.cpp:119:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |     for(int j = 0; j < b[i].size(); j ++) b[i][j] = change(b[i][j]);
      |                    ~~^~~~~~~~~~~~~
selling_rna.cpp:80:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:81:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 10320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 517 ms 234568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 11716 KB Output is correct
2 Incorrect 20 ms 11980 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 10320 KB Output isn't correct
2 Halted 0 ms 0 KB -