cf1367D

D. 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 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.InputThe 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.OutputOutput 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.

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

题意:
根据数列排列字母,每个位置上的数字都为到字典序最大的字母的位置的和。

题解:
那么数字为0时的位子放最大的字母,然后将每个位子减去到该位子的距离,(如果有两个0,就减两次),找到下一个0,结束。

运算如下
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;

int main() { 	
    int T;
    cin >> T;
    while(T--) {
 	 string s;
 	 cin >> s;
	 map<char, int> m;
 	 for (int i = 0; i < s.size(); i++) m[s[i]]++;
 	 int n;
 	 cin >> n;
 	 vector<int> v(n);
 	 for (int j = 0; j < n; j++) cin >> v[j];
 	 bool a = true;
	 vector<char> ans(n);
	 map<char, int>::iterator it = m.end();
	 while (a) {
 	     int ct = 0;
 	     vector<int> in;
  	     for (int j = 0; j < n; j++) {
   		 if (v[j] == 0) {//找到最大的字母的位置
    			 in.push_back(j);
 			 ct++;
  		         v[j] = -1;
 		 }
   	    }
  	    if (ct == 0) a = false;
  	    else {
    	         --it;
   	         while (it->second < ct)it--;//使it指向最大的字母
   	         for (int j = 0; j < in.size(); j++) ans[in[j]] = it->first;
 	    }
  	    for (int j = 0; j < n; j++) {
   	        if (v[j] != -1) {//进行运算
      		    for (int i = 0; i < in.size(); i++) v[j] -= abs(in[i] - j);
   	       }
   	    }
       }
       for (int i = 0; i < ans.size(); i++) cout << ans[i];
       cout << endl;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值