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.

思路:对于一个字符串,肯定有最大的字母和次大的字母等等,对于一开始的b数组来说,如果为0的话,那么这个位置一定是最大字母,然后再减去这个位置对于其他位置的贡献,下一个为0的位置一定就是次大的字母,一直这样弄下去直到所有的位置都弄完。
思路很好想,但是实现起来有些困难。
①对于很多个0,怎么计算所有的0的贡献。
②对于一个字母来说,这个字母用完之后,大于等于这个字母的字母个数都要清零。
这是我遇到的两个坑点。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e2+10;
int vis[maxx],b[maxx];
char s1[maxx];
string s;
int n;inline bool cmp(char a,char b)
{return a>b;
}
int main()
{int t;scanf("%d",&t);while(t--){cin>>s;scanf("%d",&n);for(int i=0;i<26;i++) vis[i]=0;for(int i=1;i<=n;i++) scanf("%d",&b[i]);for(int i=0;i<s.length();i++) vis[s[i]-'a']++;sort(s.begin(),s.end());int cnt=0;while(cnt<n){int num=0;for(int i=1;i<=n;i++) if(b[i]==0) num++;int pos;for(int i=25;i>=0;i--){if(vis[i]>=num){pos=i;break;}}int rr=num;for(int i=pos;i<26;i++) vis[i]=0;num=0;for(int i=1;i<=n;i++){if(b[i]==0){num+=i;s1[i]=(char)('a'+pos);}}int zz=0,z1=0;for(int i=1;i<=n;i++){if(b[i]==0) zz+=i,z1++,b[i]=-1,cnt++;else if(b[i]==-1) continue;else{b[i]-=((i*z1-zz)+(num-zz)-i*(rr-z1));}}//for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl;}for(int i=1;i<=n;i++) cout<<s1[i];cout<<endl;}return 0;
}

努力加油a啊,(o)/~

Task On The Board CodeForces - 1367D(思维)相关推荐

  1. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  2. Interesting Array CodeForces - 483D(思维+线段树)

    We'll call an array of n non-negative integers a[1], a[2], -, a[n] interesting, if it meets m constr ...

  3. codeforces - 1315C - 思维题

    原题链接:https://codeforces.com/problemset/problem/1315/C 翻译: 这是一个猜谜游戏,你需要猜中一个序列,谜题是一个序列,我们设为b,长度为n.你需要根 ...

  4. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  5. Codeforces 1093C (思维+贪心)

    题面 传送门 题目大意: 有一个长n(n为偶数)的序列a 已知a满足 \(a_1≤a_2≤⋯≤a_n\) 给出一个长度为\(\frac{n}{2}\) 的序列b,定义\(b_i=a_i+a_{n-i+ ...

  6. B. Bogosort codeforces(思维)

    outputstandard output You are given an array a1,a2,-,an. Array is good if for each pair of indexes i ...

  7. Count Subrectangles CodeForces - 1323B(思维)

    You are given an array a of length n and array b of length m both consisting of only integers 0 and ...

  8. Dead Pixel CodeForces - 1315A(思维)

    Screen resolution of Polycarp's monitor is a×b pixels. Unfortunately, there is one dead pixel at his ...

  9. Three Integers CodeForces - 1311D(思维+暴力)

    You are given three integers a≤b≤c. In one move, you can add +1 or −1 to any of these integers (i.e. ...

最新文章

  1. 导入eclipse项目运行时run as no application
  2. 如何在剃须刀中使用三元运算符(特别是在HTML属性上)?
  3. Mybatis的@Param注解作用
  4. 计算机大赛横幅标语有趣的,有趣的横幅标语
  5. html overflow 样式,css修改overflow滚动条默认样式
  6. Java工程师需要掌握哪些知识和专业技能呢?
  7. Servlet的九大内置对象
  8. PMP学习笔记 第10章 项目沟通管理
  9. 软考软件测评师知识点总结
  10. 【JAVA面试题-阿辉】try catch finally , try 里有 return , finally 还执行么?
  11. python :tushare 唐奇安通道
  12. mysql文件上传漏洞_文件上传漏洞
  13. 学栈和队列时的人生感悟
  14. mongoose 更新保存数据的时候自动插入__v
  15. 网络协议 -- IP地址
  16. Android NFC开发详解 总结和NFC读卡实例解析
  17. Selenium:动态页面模拟点击
  18. 中望CAD机械版学习-1-基础操作
  19. 【20210805】【数据分析】标称型数据和数值型数据
  20. 国内外dspace著名案例

热门文章

  1. UDP通讯接收案例(组播方式)
  2. 在TreeView查找某一节点
  3. python一到10整数的平方和_零基础学python_10_列表(创建数值列表 )
  4. python能干啥、实际生活-学习Python可以做什么?从事哪些岗位?
  5. 机器学习中数据集的拆分
  6. 移植根文件系统到linux内核 s3c2440,u-boot-2011.06在基于s3c2440开发板的移植之引导内核与加载根文件系统...
  7. 如何处理几十万条并发数据_Swoole 如何处理高并发以及异步 I/O 的实现
  8. Android开发面试题之activity生命周期变化
  9. 后端拼接html能做判断吗,怎么判断是前端bug还是后端bug?
  10. excel获取mysql数据库数据类型_js如何读取excel数据库数据库数据类型