Task On The Board

Polycarp wrote on the board a string s s s 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 s s s, and he rewrote the remaining letters in any order. As a result, he got some new string t t t. You have to find it with some additional information.

Suppose that the string t t t has length m m m and the characters are numbered from left to right from 1 1 1 to m m m. You are given a sequence of m integers: b 1 b_1 b1, b 2 b_2 b2, … … , b m b_m bm, where bi is the sum of the distances ∣ i − j ∣ |i−j| ij from the index i i i to all such indices j j j that t j > t i t_j>t_i tj>ti (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate b i b_i bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than t i t_i ti and sums all the values ∣ i − j ∣ |i−j| ij.

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

since t 1 = t_1= t1=‘a’, all other indices contain letters which are later in the alphabet, that is: b 1 = ∣ 1 − 2 ∣ + ∣ 1 − 3 ∣ + ∣ 1 − 4 ∣ = 1 + 2 + 3 = 6 b_1=|1−2|+|1−3|+|1−4|=1+2+3=6 b1=12+13+14=1+2+3=6;
since t 2 = t_2= t2=‘b’, only the index $j=$3 contains the letter, which is later in the alphabet, that is: b 2 = ∣ 2 − 3 ∣ = 1 b_2=|2−3|=1 b2=23=1;
since t 3 = t_3= t3=‘z’, then there are no indexes j j j such that t j > t i t_j>t_i tj>ti, thus b 3 = 0 b_3=0 b3=0;
since t 4 = t_4= t4=‘b’, only the index j = 3 j=3 j=3 contains the letter, which is later in the alphabet, that is: b 4 = ∣ 4 − 3 ∣ = 1 b_4=|4−3|=1 b4=43=1.
Thus, if t = t = t=“abzb”, then b = [ 6 , 1 , 0 , 1 ] b=[6,1,0,1] b=[6,1,0,1].

Given the string s s s and the array b b b, find any possible string t t t for which the following two requirements are fulfilled simultaneously:

t t t is obtained from s s s by erasing some letters (possibly zero) and then writing the rest in any order;
the array, constructed from the string t t t according to the rules above, equals to the array b b b specified in the input data.
Input
The first line contains an integer q ( 1 ≤ q ≤ 100 ) q (1≤q≤100) q(1q100) — the number of test cases in the test. Then q q q test cases follow.

Each test case consists of three lines:

the first line contains string s s s, which has a length from 1 1 1 to 50 50 50 and consists of lowercase English letters;
the second line contains positive integer m ( 1 ≤ m ≤ ∣ s ∣ ) m (1≤m≤|s|) m(1ms), where ∣ s ∣ |s| s is the length of the string s s s, and m m m is the length of the array b b b;
the third line contains the integers b 1 , b 2 , … , b m ( 0 ≤ b i ≤ 1225 ) b_1,b_2,…,b_m (0≤b_i≤1225) b1,b2,,bm(0bi1225).
It is guaranteed that in each test case an answer exists.

Output
Output q q q lines: the k k k-th of them should contain the answer (string t t t) to the k k k-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 t t t are suitable: "aac’, “aab”.

In the second test case, such trings t t t are suitable: “a”, “b”, “c”.

In the third test case, only the string t t t 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值