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 m integers: b1b_1,b2b_2,,bmb_m, where bi is the sum of the distances ij|i−j| from the index ii to all such indices jj that tj>tit_j>t_i (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate bib_i, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than tit_i and sums all the values ij|i−j|.

For example, if t=t = “abzb”, then:

since t1=t_1=‘a’, all other indices contain letters which are later in the alphabet, that is: b1=12+13+14=1+2+3=6b_1=|1−2|+|1−3|+|1−4|=1+2+3=6;
since t2=t_2=‘b’, only the index $j=$3 contains the letter, which is later in the alphabet, that is: b2=23=1b_2=|2−3|=1;
since t3=t_3=‘z’, then there are no indexes jj such that tj>tit_j>t_i, thus b3=0b_3=0;
since t4=t_4=‘b’, only the index j=3j=3 contains the letter, which is later in the alphabet, that is: b4=43=1b_4=|4−3|=1.
Thus, if t=t =“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 q(1q100)q (1≤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 m(1ms)m (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,,bm(0bi1225)b_1,b_2,…,b_m (0≤b_i≤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
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
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.
拓撲排序。。

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 55;
char a[N];
int b[N], T, n, m, cnt[30], ans[N];
vi tmp1, tmp2;
vi s;

int main() {
    T = qr();
    while (T--) {
        ss(a + 1), n = strlen(a + 1);
        ms(cnt);
        repi(i, 1, n)cnt[a[i] - 'a' + 1]++;
        s.clear();
        repi(i, 1, 26)if (cnt[i])s.pb(i);
        m = qr();
        repi(i, 1, m)b[i] = qr();
        tmp1.clear();
        repi(i, 1, m)if (!b[i])tmp1.pb(i);
        int now = s.size() - 1;
        while (!tmp1.empty()) {
            while (cnt[s[now]] < tmp1.size())now--;
            for (auto it:tmp1)ans[it] = s[now];
            now--;
            tmp2.clear();
            for (auto it:tmp1)
                repi(i, 1, m)if (b[i]) {
                        b[i] -= abs(it - i);
                        if (!b[i])tmp2.pb(i);
                    }
            swap(tmp1, tmp2);
        }
        repi(i, 1, m)pc(ans[i] - 1 + 'a');
        puts("");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章