#650 (Div. 3)D. Task On The Board

题目描述

Polycarp wrote on the board a string 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, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information.
Suppose that the string t has length m and the characters are numbered from left to right from 1 to m. You are given a sequence of m integers: b1,b2,…,bm, where bi is the sum of the distances |i−j| from the index i to all such indices j that tj>ti (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than ti and sums all the values |i−j|.
For example, if t = “abzb”, then:
since t1=‘a’, all other indices contain letters which are later in the alphabet, that is: b1=|1−2|+|1−3|+|1−4|=1+2+3=6;
since t2=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b2=|2−3|=1;
since t3=‘z’, then there are no indexes j such that tj>ti, thus b3=0;
since t4=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b4=|4−3|=1.
Thus, if t = “abzb”, then b=[6,1,0,1].
Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously:
t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order;
the array, constructed from the string t according to the rules above, equals to the array b specified in the input data.

Input

The first line contains an integer q (1≤q≤100) — the number of test cases in the test. Then q test cases follow.
Each test case consists of three lines:
the first line contains string s, which has a length from 1 to 50 and consists of lowercase English letters;
the second line contains positive integer m (1≤m≤|s|), where |s| is the length of the string s, and m is the length of the array b;
the third line contains the integers b1,b2,…,bm (0≤bi≤1225).
It is guaranteed that in each test case an answer exists.

Output

Output q lines: the k-th of them should contain the answer (string t) to the 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 are suitable: "aac’, “aab”.
In the second test case, such trings t are suitable: “a”, “b”, “c”.
In the third test case, only the string t equals to “aba” is suitable, but the character ‘b’ can be from the second or third position.

题目大意

一段由小写字母a-z组成的字符串s,你将从中删除几个字母并且随机排序得到字符串t。对于字符串t中的字符t[i]有着与之相对应的b[i],b[i]为字符串t中除了t[i]本身,大于t[i]的字符的索引值与i的差的绝对值之和。现在已知字符串s,t的长度和b[i]。求字符串t。

题目分析
  1. 首先找到b[i]=0的位置,因为b[i]=0,根据题意,该位置的字母肯定是s中最大的。
  2. 再让其他位置的数都减去abs(i-j),这样便又能找到新的b[i]=0的位置,那么该位置的字母便是s中第二大的数。
  3. 以此类推,便能找出最后的答案。
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=105;
int a[N],b[N];	//a储存字符串s中的字母,b为题目中的b数组
bool st[N];		//记录该位置是否被用过了
char ans[N];	//答案串
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof a);
		memset(st,0,sizeof st);
		memset(ans,0,sizeof ans);
		string s;
		int n;
		cin>>s;
		cin>>n;
		for(int i=0;i<n;i++)
		cin>>b[i];
		
		for(int i=0;i<s.size();i++)	//将s中的字符储存在a中
		{
			a[s[i]-'a']++;
		}
		int num=0,k=26;		//k记录当前的最大字符
		while(num!=n)
		{
			int cnt=0;		//统计这一轮b[i]==0的个数
			for(int i=0;i<n;i++)
			if(!st[i]&&b[i]==0) cnt++;
			
			for(int i=k-1;i>=0;i--)
			if(a[i]>=cnt) {k=i; break;}
			
			for(int i=0;i<n;i++)
			if(!st[i]&&b[i]==0)
			{
				num++;
				st[i]=true;
				ans[i]=k+'a';
			}
			
			for(int i=0;i<n;i++)
			if(b[i]==0&&ans[i]==k+'a')
			{
				for(int j=0;j<n;j++)
				if(b[j]) b[j]-=abs(i-j);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lwz_159

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

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

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

打赏作者

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

抵扣说明:

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

余额充值