2022-2023 ICPC East Central North America Regional Contest (ECNA 2022) | JorbanS

D - Determining Nucleotide Assortments

string A = "ATGC";
int a[4][N];

void solve() {
    string s; cin >> s;
    int n = s.size();
    for (int i = 1; i <= n; i ++) {
        for (int j = 0; j < 4; j ++) {
            a[j][i] = a[j][i - 1];
            if (s[i - 1] == A[j]) a[j][i] ++;
        }
    }
    int m; cin >> m;
    while (m --) {
        int l, r; cin >> l >> r;
        int cnt[4];
        set<pii> st;
        for (int i = 0; i < 4; i ++) {
            cnt[i] = a[i][r] - a[i][l - 1];
            st.insert({cnt[i], 3 - i});
        }
        string ans;
        for (auto i : st) ans += A[3 - i.bb];
        for (int i = 3; i >= 0; i --) cout << ans[i];
        cout << endl;
    }
}

G - Pea Pattern

void solve() {
    cin >> a >> b;
    if (a.size() > 100 || b.size() > 100) {
        cout << "I'm bored" << endl;
        return;
    }
    map<string, bool> mp;
    string t = a;
    int idx = 1;
    while (!mp[t]) {
        if (t == b) {
            cout << idx << endl;
            return;
        }
        idx ++;
        mp[t] = true;
        int cnt[10] = {0};
        for (int i = 0; i < t.size(); i ++) cnt[t[i] - '0'] ++;
        t = "";
        for (int i = 0; i < 10; i ++)
            if (cnt[i]) t += to_string(cnt[i]) + char('0' + i);
    }
    cout << "Does not appear" << endl;
}

I - Road To Savings

const int N = 102;
int n, m, a, b, ans, minn = 2e9;
int g[N][N];
vector<vector<int>> e(N);
int arr[N], idx;
bool vis[N];
bool used[N][N];

void dfs(int u, int d) {
    vis[u] = true;
    if (u == b && d == minn) {
        for (int i = 1; i <= idx; i ++) {
            int A = arr[i - 1], B = arr[i];
            if (A > B) swap(A, B);
            used[A][B] = true;
        }
        vis[u] = false;
        return;
    }
    if (d >= minn) {
        vis[u] = false;
        return;
    }
    for (auto v : e[u]) {
        if (vis[v]) continue;
        arr[++ idx] = v;
        dfs(v, d + g[u][v]);
        idx --;
    }
    vis[u] = false;
}

int solve() {
    cin >> n >> m >> a >> b;
    int res = 0;
    while (m --) {
        int u, v, l; cin >> u >> v >> l;
        e[u].push_back(v), e[v].push_back(u);
        g[u][v] = g[v][u] = l;
        res += l;
    }
    queue<int> q;
    vector<int> dis(n + 1, -1);
    q.push(a);
    dis[a] = 0;
    while (!q.empty()) {
        auto u = q.front();
        q.pop();
        for (auto v : e[u]) {
            if (dis[v] != -1 && dis[v] < dis[u] + g[u][v]) continue;
            dis[v] = dis[u] + g[u][v];
            if (v == b) {
                if (dis[v] < minn) minn = dis[v];
            } else q.push(v);
        }
    }
    arr[0] = a;
    dfs(a, 0);
    for (int i = 1; i <= n; i ++)
        for (int j = i + 1; j <= n; j ++)
            ans += g[i][j] * used[i][j];
    return res - ans;
}

你可能感兴趣的:(OI,题解,深度优先,算法,图论)