Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- ll power(ll a, ll b, ll x){
- ll result = 1;
- while(b > 0) {
- if(result * a > x) {
- result *= a;
- break;
- }
- result *= a;
- b--;
- }
- return result;
- }
- int main() {
- ll n, m, k, x;
- cin >> n >> m >> k >> x;
- vector<string> v;
- string S;
- cin >> S;
- for(int i = 0; i < m; i++) {
- string tmp;
- cin >> tmp;
- sort(tmp.begin(), tmp.end());
- v.push_back(tmp);
- }
- vector<char> result;
- for(ll i = 0; i < m; i++) {
- int j = 0;
- ll k_power = power(k, m - i - 1, x);
- ll sum = 0;
- while(sum + k_power < x) {
- sum += k_power;
- j++;
- }
- x -= sum;
- result.push_back(v[i][j]);
- }
- int idx = 0;
- for(int i = 0; i < S.size(); i++) {
- if(S[i] == '#') {
- S[i] = result[idx];
- idx++;
- }
- }
- cout << S << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement