D. Task On The Board

Polycarp wrote on the board a string ss containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input.

After that, he erased some letters from the string ss, and he rewrote the remaining letters in any order. As a result, he got some new string tt. You have to find it with some additional information.

Suppose that the string tt has length mm and the characters are numbered from left to right from 11 to mm. You are given a sequence of mm integers: b1,b2,,bmb1,b2,…,bm, where bibi is the sum of the distances |ij||i−j| from the index ii to all such indices jj that tj>titj>ti (consider that 'a'<'b'<...<'z'). In other words, to calculate bibi, Polycarp finds all such indices jj that the index jj contains a letter that is later in the alphabet than titi and sums all the values |ij||i−j|.

For example, if tt = "abzb", then:

  • since t1t1='a', all other indices contain letters which are later in the alphabet, that is: b1=|12|+|13|+|14|=1+2+3=6b1=|1−2|+|1−3|+|1−4|=1+2+3=6;
  • since t2t2='b', only the index j=3j=3 contains the letter, which is later in the alphabet, that is: b2=|23|=1b2=|2−3|=1;
  • since t3t3='z', then there are no indexes jj such that tj>titj>ti, thus b3=0b3=0;
  • since t4t4='b', only the index j=3j=3 contains the letter, which is later in the alphabet, that is: b4=|43|=1b4=|4−3|=1.

Thus, if tt = "abzb", then b=[6,1,0,1]b=[6,1,0,1].

Given the string ss and the array bb, find any possible string tt for which the following two requirements are fulfilled simultaneously:

  • tt is obtained from ss by erasing some letters (possibly zero) and then writing the rest in any order;
  • the array, constructed from the string tt according to the rules above, equals to the array bb specified in the input data.
Input

The first line contains an integer qq (1q1001≤q≤100) — the number of test cases in the test. Then qq test cases follow.

Each test case consists of three lines:

  • the first line contains string ss, which has a length from 11 to 5050 and consists of lowercase English letters;
  • the second line contains positive integer mm (1m|s|1≤m≤|s|), where |s||s| is the length of the string ss, and mm is the length of the array bb;
  • the third line contains the integers b1,b2,,bmb1,b2,…,bm (0bi12250≤bi≤1225).

It is guaranteed that in each test case an answer exists.

Output

Output qq lines: the kk-th of them should contain the answer (string tt) to the kk-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any.

Example
input
Copy
4
abac
3
2 1 0
abc
1
0
abba
3
1 0 1
ecoosdcefr
10
38 13 24 14 11 5 3 24 17 0
output
Copy
aac
b
aba
codeforces
Note

In the first test case, such strings tt are suitable: "aac', "aab".

In the second test case, such trings tt are suitable: "a", "b", "c".

In the third test case, only the string tt equals to "aba" is suitable, but the character 'b' can be from the second or third position.

 如果数字是0那只有放最大的

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 3e3 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
ll qpow(ll m, ll k, ll mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}
double dp[N][N];
double a[N],ans;
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        string s;
        cin >> s;
        int n;
        cin >> n;
        vector<int> a(n);
        vector<int> cnt(26);
        vector<char> res(n);
        vector<bool> vis(n);
        forn(i, n)
            cin >> a[i];
        forn(i, s.size())
            cnt[s[i] - 'a']++;
        int t = 0,pos=25;
        while(t<n)
        {
            vector<int> v;
            forn(i, n)
            {
                if (a[i] == 0 && !vis[i])
                {
                    v.push_back(i);
                    vis[i] = 1;
                }
            }
            while (cnt[pos] < v.size())
                pos--;
            cnt[pos] -= v.size();
 
            forn(i, v.size())
            {
                forn(j, n)
                {
                    if (a[j])
                    {
                        a[j] -= abs(j - v[i]);
                    }
                }
                res[v[i]] = pos + 'a';
                t++;
            }
            pos--;
        }
        forn(i, n)
            cout << res[i];
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/dealer/p/13176174.html