답안 #748450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
748450 2023-05-26T09:49:28 Z guagua0407 Selling RNA Strands (JOI16_selling_rna) C++17
0 / 100
326 ms 536084 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=2e6+5;
string S[mxn];
string rev[mxn];
string T1[mxn],T2[mxn];
int trie[mxn][4];
int trie2[mxn][4];
int cnt[mxn*4];
int ans[mxn];
vector<int> query[mxn*4];
map<char,int> mp;
int cur=0;

void insert(string str){
    int v=0;
    for(int i=0;i<str.length();i++){
        int a=mp[str[i]];
        if(trie[v][a]==-1){
            cur++;
            trie[v][a]=cur;
            v=cur;
        }
        else{
            v=trie[v][a];
        }
    }
}

void insert1(string str,int id){
    int v=0;
    for(int i=0;i<str.length() and v!=-1;i++){
        int a=mp[str[i]];
        v=trie[v][a];
    }
    if(v!=-1){
        query[v].push_back(id);
    }
}

void upd(string str,int d){
    int v=0;
    for(int i=0;i<str.length();i++){
        int a=mp[str[i]];
        if(trie2[v][a]==-1){
            cur++;
            trie2[v][a]=cur;
            v=cur;
        }
        else{
            v=trie2[v][a];
        }
        cnt[v]+=d;
    }
}

int query2(string str){
    int v=0;
    for(int i=0;i<str.length() and v!=-1;i++){
        int a=mp[str[i]];
        v=trie2[v][a];
    }
    if(v==-1) return 0;
    return cnt[v];
}

void dfs(int v,vector<int> vec,int ord=0){
    vector<int> num[4];
    for(auto u:vec){
        if(S[u].size()<=ord){
            continue;
        }
        num[mp[S[u][ord]]].push_back(u);
    }
    for(int i=0;i<4;i++){
        if(num[i].empty()) continue;
        dfs(trie[v][i],num[i],ord+1);
    }
    for(auto u:vec){
        if(S[u].size()<=ord){
            upd(rev[u],1);
        }
    }
    for(auto u:query[v]){
        ans[u]=query2(T2[u]);
    }
}

int main() {_
    mp['A']=0;
    mp['G']=1;
    mp['C']=2;
    mp['U']=3;
    for(int i=0;i<mxn;i++){
        for(int j=0;j<4;j++){
            trie[i][j]=trie2[i][j]=-1;
        }
    }
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>S[i];
        rev[i]=S[i];
        reverse(all(rev[i]));
        insert(S[i]);
    }
    for(int i=0;i<m;i++){
        cin>>T1[i]>>T2[i];
        reverse(all(T2[i]));
        insert1(T1[i],i);
    }
    vector<int> vec(n);
    for(int i=0;i<n;i++){
        vec[i]=i;
    }
    dfs(0,vec);
    for(int i=0;i<m;i++){
        cout<<ans[i]<<'\n';
    }
    return 0;
}
//maybe its multiset not set

Compilation message

selling_rna.cpp: In function 'void insert(std::string)':
selling_rna.cpp:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for(int i=0;i<str.length();i++){
      |                 ~^~~~~~~~~~~~~
selling_rna.cpp: In function 'void insert1(std::string, int)':
selling_rna.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(int i=0;i<str.length() and v!=-1;i++){
      |                 ~^~~~~~~~~~~~~
selling_rna.cpp: In function 'void upd(std::string, int)':
selling_rna.cpp:56:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for(int i=0;i<str.length();i++){
      |                 ~^~~~~~~~~~~~~
selling_rna.cpp: In function 'int query2(std::string)':
selling_rna.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |     for(int i=0;i<str.length() and v!=-1;i++){
      |                 ~^~~~~~~~~~~~~
selling_rna.cpp: In function 'void dfs(int, std::vector<int>, int)':
selling_rna.cpp:83:23: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   83 |         if(S[u].size()<=ord){
      |            ~~~~~~~~~~~^~~~~
selling_rna.cpp:93:23: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   93 |         if(S[u].size()<=ord){
      |            ~~~~~~~~~~~^~~~~
selling_rna.cpp: In function 'void setIO(std::string)':
selling_rna.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 236 ms 501256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 326 ms 536084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 502844 KB Output is correct
2 Incorrect 236 ms 502676 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 236 ms 501256 KB Output isn't correct
2 Halted 0 ms 0 KB -