当前位置: 代码迷 >> 综合 >> Codeforces D. Task On The Board (思维 / 构造) (Round #650 Div.3)
  详细解决方案

Codeforces D. Task On The Board (思维 / 构造) (Round #650 Div.3)

热度:95   发布时间:2023-12-22 13:44:29.0

传送门

题意: 现有一个初始字符串a,可在其上删除一些字符(也可以不删)得到中间字符串t。而t含有m个字符,对应符合一个标准数组b;使得b[i]正好等于t[i]与t中其他比t[i]字典序大的字符索引j的 |i - j| 的和。先给出a字符串与b数组,需构造出中间字符串t。
在这里插入图片描述
思路:

  • 因为b[i] = 0的时候表示该字符是字符串中最大的字符,否则会有更大的字符对其产生影响(其值必不可能为0),所以我们就每次都以b[i] = 0为特殊点开始处理。
  • 首先找出足够数目的最大字符来填充所以b[i] = 0的位置,并用vis[i] = 1来标记该位置已被填充。
  • 那么其余b[i] != 0的位置就会被该字符影响,减去这个影响值即可(eg:b[i] = 0,那么对于其他b[j] != 0,则有b[j] -= |i - j|)
  • 于是就会出现新的b[i] = 0,再重复以上方法最后得到答案字符串即可。

代码实现:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
    {
    1, 0}, {
    -1, 0}, {
    0, 1}, {
    0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2000;int T, m;
char a[N], t[N];
int b[N], vis[N], cnt[N];signed main()
{
    IOS;cin >> T;while(T --){
    me(cnt); me(vis);cin >> (a + 1) >> m;for(int i = 1; i <= m; i ++)cin >> b[i];int la = strlen(a + 1);for(int i = 1; i <= la; i ++) //统计每个字符的数量cnt[a[i] - 'a'] ++;//因为每次都得向下找到最大字符,所有得用bg来记录每次寻找开始得位置int ok = 1, res = 0, bg = 25;while(ok){
    int num = 0, tmp = -1;//统计当前b[i] = 0的数量for(int i = 1; i <= m; i ++)if(!b[i] && !vis[i]) num ++;//找到最大的字符能够填充所有b[i] = 0的位置for(int i = bg; ~i; i --){
    if(cnt[i] >= num){
    tmp = i;break;}}int tt = 0, hh = 0;//开始填充for(int i = 1; i <= m; i ++){
    if(!b[i] && !vis[i]){
    vis[i] = 1;//统计已构造字符串的长度res ++;//将找到的字符tmp复制给t[i]t[i] = tmp + 'a';//减去i位置对后面的影响for(int j = 1; j < i; j ++)if(b[j]) b[j] -= (i - j);hh += i; tt ++;}//减去前面对i位置的影响else if(b[i]) b[i] -= (tt * i - hh);}//下一次字符的寻找从tmp - 1开始bg = tmp - 1;//m长度的字符串已构造完成if(res == m) break;}for(int i = 1; i <= m; i ++)cout << t[i];cout << endl;}return 0;
}
  相关解决方案