(200628) 코드포스

Codeforces materials usage license (v. 0.1) 에 따라 codeforces.com 의 문제임을 알립니다.

Original problem link: Problem - 1367D - Codeforces


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


Common codes for Codeforces
#include <algorithm>
#include <array>
#include <cmath>
#include <functional> 
#include <iostream>
#include <queue>
#include <string>
#include <vector>

namespace predefined {

#ifdef CODEFORCES_DEBUG
  constexpr bool kIsDebug = true;
#else
  constexpr bool kIsDebug = false;
#endif

enum class LogType {
  INFO,
  DEBUG
};

template <LogType type, bool... Bools>
struct Log;

template <>
struct Log<LogType::INFO> {
  Log() {}
  ~Log() { std::cout << '\n'; }

  template <typename T>
  Log& operator<< (const T &t) {
    std::cout << t;
    return *this;
  }
};

template <>
struct Log<LogType::DEBUG, true>{
  Log() {}
  ~Log() { std::cout << '\n'; }

  void Open() {
    freopen("input.txt", "r", stdin);
  }

  template <typename T>
  Log& operator<< (const T &t) {
    std::cout << t;
    return *this;
  }
};

template <>
struct Log<LogType::DEBUG, false> {
  Log() {}
  ~Log() {}

  void Open() {}

  template <typename T>
  Log& operator<< (const T &t) { return *this; }
};

template<typename T>
struct BoolMap {
  explicit BoolMap(const T &_true_value, const T &_false_value) 
    : true_value(_true_value),
      false_value(_false_value) {}

  const T true_value;
  const T false_value;

  const T inline GetValue(const bool bool_key) const {
    return bool_key ? true_value : false_value;  
  };
};

}  // predefined;

#define LOG_INFO predefined::Log<predefined::LogType::INFO>()
#define LOG_DEBUG predefined::Log<predefined::LogType::DEBUG, predefined::kIsDebug>()
#define LOG(LEVEL) LOG_##LEVEL
#define LABEL(variable_name) #variable_name << ": " << variable_name

#define INIT_ANSWER(type, true_value, false_value) \
  const predefined::BoolMap<type> predefined_bool_map(true_value, false_value)
#define INIT_STRING_ANSWER(true_string, false_string) \
  INIT_ANSWER(std::string, true_string, false_string)
#define GET_ANSWER(bool_key) \
  predefined_bool_map.GetValue(bool_key)

#define INIT_CODEFORCES() \
  std::ios::sync_with_stdio(false); \
  std::cin.tie(NULL); \
  LOG(DEBUG).Open(); \

1 Like
1367D
namespace {
using namespace std;
}  // unnamed namespace;

int main(int argc, char** argv) {
  INIT_CODEFORCES();

  int test_case;
  cin >> test_case;

  while (test_case--) {
    string input;
    cin >> input;
    
    vector<int> count(256,0);
    for (auto c : input) {
      ++count[c];
    }

    int b;

    cin >> b;
    vector<int> seq(b);
    for (int i = 0; i < b; ++i) {
      cin >> seq[i];
    }

    string ans(b, 0);
    char count_index = 'z' + 1;

    vector<int> candis;

    while (--count_index >= 'a') {
      if (count[count_index] == 0) continue;
      
      if(candis.size() == 0) {
        for (int i = 0; i < b; ++i) {
          if (ans[i] != 0) continue;
          
          int dist = 0;
          for (int j = 0; j < b; ++j) {
            if (ans[j] != 0) dist += abs(i - j);
          }

          if (dist == seq[i]) {
            candis.emplace_back(i);
          }
        }
      } 

      if (count[count_index] >= candis.size()) {
        for (const auto &candi : candis) ans[candi] = count_index;
        candis.clear();
      }
    }

    LOG_INFO << ans;
  }

  return 0;
}