_rashed

SRM405-D2-1000

Feb 21st, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. int mem[101][101];
  9.  
  10. class IdealString {
  11.     public:
  12.     int req;
  13.     vector<int> seq;
  14.     int solve(int last, int sum) {
  15.         if(sum > req) {
  16.             return -2;
  17.         }
  18.         if(sum == req) {
  19.             return 0;
  20.         }
  21.         if(mem[last][sum] != -1)
  22.             return mem[last][sum];
  23.         int &ret = mem[last][sum];
  24.         ret = -2;
  25.         for(int i = sum+1; i >= last+1; i--) {
  26.             if(solve(i,sum+i) != -2) {
  27.                 ret = i;
  28.                 break;
  29.             }
  30.         }
  31.         return ret;
  32.     }
  33.  
  34.  
  35.  
  36.     void pre() {
  37.         memset(mem,-1,sizeof(mem));
  38.         seq.clear();
  39.     }
  40.  
  41.     void trace(int last, int sum) {
  42.         if(sum == req)
  43.             return;
  44.         int curr = solve(last,sum);
  45.         seq.push_back(curr);
  46.         trace(curr,sum+curr);
  47.     }
  48.  
  49.     string construct(int length) {
  50.         pre();
  51.         req = length;
  52.         int p = solve(1,1);
  53.         if(p == -2) {
  54.             return "";
  55.         }
  56.         seq.push_back(1);
  57.         trace(1,1);
  58.         string ans(length,'x');
  59.         char curr = 'A';
  60.         int num[26];
  61.         for(int i = 0; i < seq.size(); i++) {
  62.             ans[seq[i]-1] = curr;
  63.             num[curr-'A'] = seq[i]-1;
  64.             curr++;
  65.         }
  66.         int char_idx = 0;
  67.         curr = 'A';
  68.         for(int i = 0; i < length; i++) {
  69.             if(!num[char_idx]) {
  70.                 char_idx++;
  71.                 curr++;
  72.             }
  73.             if(ans[i] == 'x') {
  74.                 ans[i] = curr;
  75.                 num[char_idx]--;
  76.  
  77.             }
  78.         }
  79.         return ans;
  80.     }
  81.  
  82.  
  83. };
Advertisement
Add Comment
Please, Sign In to add comment