problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
acwing::acwing4307类 参考

AcWing 4307. 数字重构 更多...

#include <acwing.h>

静态 Public 成员函数

static string dfs (int i, bool free, unordered_map< int, int > um, string &b)
 
static int main (istream &cin, ostream &cout)
 

详细描述

AcWing 4307. 数字重构

在文件 acwing.h1452 行定义.

成员函数说明

◆ dfs()

string acwing::acwing4307::dfs ( int  i,
bool  free,
unordered_map< int, int >  um,
string &  b 
)
static

在文件 acwing.cpp4624 行定义.

4624 {
4625 auto oss = ostringstream();
4626 int j = b[i] - '0';
4627 if(free) {
4628 goto B;
4629 }
4630 if(um[j] > 0) {
4631 auto um_cpy = unordered_map(um);
4632 um_cpy[j]--;
4633 if(i == b.length() - 1) {
4634 oss << j;
4635 return oss.str();
4636 }
4637 string ans = dfs(i + 1, false, um_cpy, b);
4638 if(!ans.empty()) {
4639 oss << j << ans;
4640 } else {
4641 //不可行
4642 goto B;
4643 }
4644 } else {
4645 B:;
4646 int k = b[i] - '0' - 1;
4647 if(free) {
4648 k = 9;
4649 }
4650 for(; k >= 0; k--) {
4651 if(um[k] > 0) {
4652 auto um_cpy = unordered_map(um);
4653 um_cpy[k]--;
4654 if(i == b.length() - 1) {
4655 oss << k;
4656 break;
4657 }
4658 string ans = dfs(i + 1, true, um_cpy, b);
4659 if(!ans.empty()) {
4660 oss << k << ans;
4661 break;
4662 }
4663 }
4664 }
4665 }
4666
4667 return oss.str();
4668 }
static string dfs(int i, bool free, unordered_map< int, int > um, string &b)
Definition: acwing.cpp:4624

◆ main()

int acwing::acwing4307::main ( istream &  cin,
ostream &  cout 
)
static

在文件 acwing.cpp4603 行定义.

4603 {
4604 string a;
4605 string b;
4606 cin >> a >> b;
4607 auto a_count = unordered_map<int, int>();
4608 for(const char ch: a) {
4609 a_count[ch - '0']++;
4610 }
4611 if(a.length() == b.length()) {
4612 cout << dfs(0, false, a_count, b);
4613 } else {
4614 for(int i = 9; i >= 0; i--) {
4615 while(a_count[i] > 0) {
4616 cout << i;
4617 a_count[i]--;
4618 }
4619 }
4620 }
4621 return 0;
4622 }

被这些函数引用 acwing::TEST().


该类的文档由以下文件生成: