Secret password#11404

Secret password (Editorial)

Solution

General Idea: The password was originally written with characters in **odd positions** stored in string , and characters in **even positions** stored in string . Both strings preserve order.

All we need to do is reconstruct the original password by merging the characters:

- Take one character from (odd positions). - Then one from (even positions). - Keep alternating. - If is longer by one character (because total length could be odd), append the last remaining character.

This gives the original password in \mathcal{O}(|t| + |c|).


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

void solve() {
    str t, c;
    cin >> t >> c;

    str ans = "";
    int i = 0, j = 0;
    while (i < (int)t.size() && j < (int)c.size()) {
        ans += t[i++];
        ans += c[j++];
    }
    while (i < (int)t.size()) ans += t[i++];

    while (j < (int)c.size()) ans += c[j++];

    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    while (T--) {
        solve();
        cout << "\n";
    }
}