D. Task On The Board

https://codeforces.com/problemset/problem/1367/D

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.ExampleinputCopy

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

outputCopy

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.



思路:刚开始可能没什么思路。但是如果发现给的b[i]中为0的,也就是最小值,肯定对应的是能够满足的最大字母(因为其他字母对他没有贡献)

然后再通过开始为0的减去对其他数字贡献,拿到新的0的,以此类推

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1300;
typedef long long LL;
char str[70];
LL b[maxn];
LL num[30];
char ans[maxn];
LL vis[maxn];
void init()
{
	memset(str,'\0',sizeof(str));
  	memset(num,0,sizeof(num));
  	memset(ans,'\0',sizeof(ans));
}
int main(void)
{
  LL q;cin>>q;
  while(q--)
  {
  	 init();	 
  	 cin>>str+1;
  	 LL n=strlen(str+1); 
  	 for(LL i=1;i<=n+10;i++) vis[i]=false,b[i]=0;
  	 LL m;cin>>m;
  	 for(LL i=1;i<=m;i++) cin>>b[i];
  	 for(LL i=1;i<=n;i++) num[str[i]-'a'+1]++;  
	 LL have=0;LL top=26;LL k=top;LL times=1;
	 while(1)
	 {
	 	 LL zero=0;
	 	 for(LL i=1;i<=m;i++)
  	 	 {
  	 	   if(!vis[i]&&b[i]==0) zero++;	
	 	 }
	 	 for(LL i=top;i>=1;i--)
	 	 {
	 	 	if(num[i]>=zero)
			  {
			  	k=i;break;	
			  }	
		 }
		 for(LL i=1;i<=m;i++) 
		 {
		 	if(!vis[i]&&b[i]==0) 
			 {
			 	ans[i]=char('a'+k-1);
			 	vis[i]=times;
			 }
		 }
		 for(LL i=1;i<=m;i++)
		 {
		 	if(vis[i]==times&&b[i]==0)
		 	{
				have++;
				for(LL j=1;j<=m;j++)
				{
					if(b[j]) 
					b[j]-=fabs(i-j);	
				}
			}
		 }
		 times++;
		 top=k-1;
		 if(have==m) break;		 
	 }
	 for(LL i=1;i<=m;i++) cout<<ans[i];
	 cout<<endl;
  }
return 0;
}
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页