답안 #1019635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019635 2024-07-11T06:14:35 Z NintsiChkhaidze Selling RNA Strands (JOI16_selling_rna) C++17
10 / 100
1500 ms 215316 KB
#include <bits/stdc++.h>
#include <istream>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
// #define int ll
using namespace std;

const int N = 2e6 + 5;
string s[N];
map <int,int> mp;
int t[N][5],cnt1 = 1,cnt2 = 1,timer1,tin1[N],tout1[N];
int timer2,tin2[N],tout2[N],S[N][5];
vector <pii> vec1,vec2;
vector <int> v1[N],v2[N];
bool fix[N];

void add1(string str,int id){
  int root = 1;
  for (int i = 0; i < str.size(); i++){
    int b = mp[str[i]];
    if (!t[root][b]){
      t[root][b] = ++cnt1;
    }
    
    root = t[root][b];
  }

  vec1.pb({root,id});
}

int getprefix(string str){
  int root = 1;
  for (int i = 0; i < str.size(); i++){
    int b = mp[str[i]];
    if (!t[root][b]) return -1;
    root = t[root][b];
  }
  return root;
}

int getsuffix(string str){
  int root = 1;
  for (int i = 0; i < str.size(); i++){
    int b = mp[str[i]];
    if (!S[root][b]) return -1;
    root = S[root][b];
  }
  return root;
}

void add2(string str,int id){
  int root = 1;
  for (int i = 0; i < str.size(); i++){
    int b = mp[str[i]];
    if (!S[root][b]){
      S[root][b] = ++cnt2;
    }
    root = S[root][b];
  }

  vec2.pb({root,id});
}

void dfs1(int x){
  tin1[x] = ++timer1;
  for (int i = 1; i <= 4; i++){
    if (t[x][i]) dfs1(t[x][i]);
  }
  tout1[x] = timer1;
}

void dfs2(int x){
  tin2[x] = ++timer2;
  for (int i = 1; i <= 4; i++){
    if (S[x][i]) dfs2(S[x][i]);
  }
  tout2[x] = timer2;
}

signed main() {
  ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

  mp['A'] = 1;
  mp['C'] = 2;
  mp['G'] = 3;
  mp['U'] = 4;

  int n,m;
  cin>>n>>m;

  for (int i = 1; i <= n; i++){
    cin >> s[i];
    add1(s[i],i);
    reverse(s[i].begin(),s[i].end());
    add2(s[i],i);
  }

  dfs1(1);
  dfs2(1);

  for (auto [x,j]: vec1){
    v1[tin1[x]].pb(j);
  }

  for (auto [x,j]: vec2){
    v2[tin2[x]].pb(j);
  }

  for (int i = 1; i <= m; i++){
    string x,y;
    cin>>x>>y;

    int A = getprefix(x);
    if (A == -1) {
      cout<<0<<endl;
      continue;
    }
    int l1 = tin1[A],r1 = tout1[A];

    reverse(y.begin(),y.end());
    int B = getsuffix(y);
    if (B == -1){
      cout<<0<<endl;
      continue;
    }
    int l2 = tin2[B],r2 = tout2[B];

    vector <int> vvv;
    // cout<<"! ";
    for (int i = l1; i <= r1; i++){
      for (int x: v1[i]) fix[x] = 1,vvv.pb(x);//cout<<x<<" ";
    }
    // cout<<endl;

    // cout<<"!! ";
    int ans = 0;
    for (int i = l2; i <= r2; i++){
      for (int x: v2[i]) ans += (fix[x]);//cout<<x<<" ";
    }
    // cout<<endl;

    cout<<ans<<endl;
    for (int k: vvv) fix[k] = 0;
  }
}

Compilation message

selling_rna.cpp: In function 'void add1(std::string, int)':
selling_rna.cpp:25:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |   for (int i = 0; i < str.size(); i++){
      |                   ~~^~~~~~~~~~~~
selling_rna.cpp: In function 'int getprefix(std::string)':
selling_rna.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |   for (int i = 0; i < str.size(); i++){
      |                   ~~^~~~~~~~~~~~
selling_rna.cpp: In function 'int getsuffix(std::string)':
selling_rna.cpp:49:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   for (int i = 0; i < str.size(); i++){
      |                   ~~^~~~~~~~~~~~
selling_rna.cpp: In function 'void add2(std::string, int)':
selling_rna.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int i = 0; i < str.size(); i++){
      |                   ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 157008 KB Output is correct
2 Correct 63 ms 156968 KB Output is correct
3 Correct 63 ms 156944 KB Output is correct
4 Correct 72 ms 157008 KB Output is correct
5 Correct 63 ms 156956 KB Output is correct
6 Correct 61 ms 157012 KB Output is correct
7 Correct 62 ms 157012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 682 ms 215316 KB Output is correct
2 Execution timed out 1510 ms 214580 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1564 ms 158676 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 157008 KB Output is correct
2 Correct 63 ms 156968 KB Output is correct
3 Correct 63 ms 156944 KB Output is correct
4 Correct 72 ms 157008 KB Output is correct
5 Correct 63 ms 156956 KB Output is correct
6 Correct 61 ms 157012 KB Output is correct
7 Correct 62 ms 157012 KB Output is correct
8 Correct 682 ms 215316 KB Output is correct
9 Execution timed out 1510 ms 214580 KB Time limit exceeded
10 Halted 0 ms 0 KB -