D. Task On The Board -构造

D. Task On The Board

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 |i−j||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 |i−j||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=|1−2|+|1−3|+|1−4|=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=|2−3|=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=|4−3|=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 (1≤q≤1001≤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 (1≤m≤|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 (0≤bi≤12250≤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是一个突破点,贪心的想,0的位置放上最大的,再用0的位置去更新没有更新的位置,这样周而复始即可,值得注意的是,我们每次的上届都应该比上一次要小,而不是单纯的找一个当前最大的

# include<bits/stdc++.h>

using namespace std;
# define mod 1000000007
typedef long long int ll;

int cnt[50];
int b[100];
int ans[100];
int main ()
{
/*
    iejkhmjeil
    hejigmjehl
*/
    int t;
    cin>>t;

    while(t--)
    {
        string s;
        cin>>s;
        int n;
        cin>>n;

        for(int i=1;i<=n;i++)
        {
            cin>>b[i];

            ans[i]=0;
        }
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<s.length();i++)
        {
           cnt[s[i]-'a'+1]++;
        }

        int up=26;
        while(1)
        {
            vector<int>v;

            for(int i=1;i<=n;i++)
            {
                if(ans[i])
                    continue;

                if(b[i]==0)
                {
                    v.push_back(i);
                }
            }

            if(v.size()==0)
            {
                break;
            }
            int temp=0;

            for(int i=up;i>=1;i--)
            {
                if(cnt[i]>=(int)v.size())
                {
                    temp=i;
                    //cnt[i]-=v.size();
                    up=i;
                    cnt[i]=0;
                    break;
                }
            }

            for(auto it:v)
            {
                //b[it]=temp;
                ans[it]=temp;
               // book[it]=1;
            }

            for(int i=1;i<=n;i++)
            {
                if(ans[i])
                    continue;

                for(auto it:v)
                {
                    b[i]-=abs(i-it);
                }
            }
        }

        for(int i=1;i<=n;i++)
        {

            cout<<(char)(ans[i]-1+'a');
        }

        cout<<endl;


    }





    return 0;

}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秦三码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值