답안 #894416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894416 2023-12-28T09:06:27 Z Cadoc Selling RNA Strands (JOI16_selling_rna) C++14
35 / 100
518 ms 45400 KB
/*
    Author: Cadocx
    Codeforces: https://codeforces.com/profile/Kadoc
    VNOJ: oj.vnoi.info/user/Cadoc
*/

#include <bits/stdc++.h>
using namespace std;

// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
// #pragma GCC optimize("O2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
//STL
#define sz(x) (int)(x).size()
#define for1(i,l,r) for(auto i = l; i <= r; i++)
#define for2(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define N 100005
#define MOD 1000000009
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0

int n, m;
string str[N];

struct Query{
    string p, q;
} qr[N];

void Init(){
    cin >> n >> m;
    for(int i=1; i<=n; ++i) cin >> str[i];
    for(int i=1; i<=m; ++i) cin >> qr[i].p >> qr[i].q;
}

bool checkSubtask1(){
    int ss = 0, sp = 0, sq = 0;
    for(int i=1; i<=n; ++i) ss += str[i].size();
    for(int i=1; i<=m; ++i) sp += qr[i].p.size(), sq += qr[i].q.size();
    return n <= 100 && m <= 100 && ss <= 100 && sp <= 100 && sq <= 100;
}

namespace subtask1{
    bool check(string a, string b, string c){
        int n = a.size(), m = b.size(), p = c.size();
        if(n > p || m > p) return 0;
        for(int i=0; i<n; ++i) if(c[i] != a[i]) return 0;
        for(int i=p-m; i<p; ++i) if(c[i] != b[i-p+m]) return 0;
        return 1;
    }

    void solve(){
        for(int i=1; i<=m; ++i){
            string p = qr[i].p, q = qr[i].q;

            int ans = 0;
            for(int j=1; j<=n; ++j) ans += check(p, q, str[j]);

            cout << ans; el;
        }
    }
}

bool checkSubtask2(){
    return n <= 5000 && m <= 5000;
}

namespace subtask2{
    const int mxN = 1e5;

    ll p[N];
    vector<ll> hs[N];

    ll get(int id, int l, int r){
        return ((hs[id][r] - hs[id][l-1] * p[r-l+1]) % MOD + MOD) % MOD;
    }

    void solve(){
        p[0] = 1;
        for(int i=1; i<=mxN; ++i) p[i] = p[i-1] * base % MOD;

        for(int i=1; i<=n; ++i){
            ll cur = 0;
            string s = str[i];
            int p = s.size();
            hs[i].pb(0);
            for(int j=0; j<p; ++j){
                cur = (cur * base + s[j] - 'A' + 1) % MOD;
                hs[i].pb(cur);
            }
        }

        for(int i=1; i<=m; ++i){
            string s = qr[i].p, t = qr[i].q;
            int p = s.size(), q = t.size();

            ll hp = 0, hq = 0;
            for(int j=0; j<p; ++j) hp = (hp * base + s[j] - 'A' + 1) % MOD;
            for(int j=0; j<q; ++j) hq = (hq * base + t[j] - 'A' + 1) % MOD;

            int ans = 0;
            for(int j=1; j<=n; ++j){
                int k = str[j].size();
                if(p > k || q > k) continue;
                ll a = get(j, 1, p), b = get(j, k-q+1, k);
                ans += (a == hp && b == hq);
            }

            cout << ans; el;
        }
    }
}

void Solve(){
    if(checkSubtask1()) return subtask1::solve();
    if(checkSubtask2()) return subtask2::solve();
}

int main(){
    #define NAME "TASK"
    if(fopen(NAME".inp", "r")){
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }

    fastIO;
    
    Init();
    Solve();
    return execute, 0;
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 3 ms 12904 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 3 ms 12988 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 40680 KB Output is correct
2 Correct 390 ms 42788 KB Output is correct
3 Correct 259 ms 41808 KB Output is correct
4 Correct 420 ms 42788 KB Output is correct
5 Correct 458 ms 31504 KB Output is correct
6 Correct 518 ms 31844 KB Output is correct
7 Correct 168 ms 39764 KB Output is correct
8 Correct 315 ms 45400 KB Output is correct
9 Correct 419 ms 45136 KB Output is correct
10 Correct 379 ms 40272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 13400 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12888 KB Output is correct
3 Correct 3 ms 12904 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 3 ms 12988 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 90 ms 40680 KB Output is correct
9 Correct 390 ms 42788 KB Output is correct
10 Correct 259 ms 41808 KB Output is correct
11 Correct 420 ms 42788 KB Output is correct
12 Correct 458 ms 31504 KB Output is correct
13 Correct 518 ms 31844 KB Output is correct
14 Correct 168 ms 39764 KB Output is correct
15 Correct 315 ms 45400 KB Output is correct
16 Correct 419 ms 45136 KB Output is correct
17 Correct 379 ms 40272 KB Output is correct
18 Incorrect 10 ms 13400 KB Output isn't correct
19 Halted 0 ms 0 KB -