#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; int DP[MAXN]; int wybor[MAXN]; int monety[] = {3, 4, 5, 6}; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; int ileJeden = 0; char x; cin >> n; for(int i = 0; i < 8*n; i++){ cin >> x; if(x == '1'){ ileJeden++; } } //cout << ileJeden << "\n"; int minimum = 0; int ind = 0; int k = 4; for(int i = 0; i <= ileJeden; i++){ wybor[i] = -1; } for(int i = 1; i <= ileJeden; i++){ minimum = ileJeden+1; for(int j = 0; j < k; j++){ if(i-monety[j] >= 0){ if(minimum > DP[i-monety[j]]+1){ minimum = DP[i-monety[j]]+1; ind = j; } } } DP[i] = minimum; wybor[i] = ind; } if(DP[ileJeden] > ileJeden || DP[ileJeden] < 0){ cout << "NIE\n"; return 0; } //odzyskiwanie vector<int> znaki; int ile = 0; int ile6 = 0; while(ileJeden > 0){ ile++; //cout << "x" << monety[wybor[ileJeden]] << "\n"; if(monety[wybor[ileJeden]] == 6){ ile6++; }else{ znaki.push_back(monety[wybor[ileJeden]]); } ileJeden = ileJeden - monety[wybor[ileJeden]]; } /*for(auto x: znaki){ cout << x << " "; } cout << "\n" << ile << "\n";*/ if(ile < n){ while(ile6 > 0 && ile < n){ ile++; ile6--; znaki.push_back(3); znaki.push_back(3); } if(ile != n){ cout << "NIE\n"; return 0; } } while(ile6 > 0){ znaki.push_back(6); ile6--; } /*for(auto x: znaki){ cout << x << " "; } cout << "\n";*/ for(int i = 0; i < znaki.size(); i++){ if(znaki[i] == 3){ cout << "a"; } if(znaki[i] == 4){ cout << "c"; } if(znaki[i] == 5){ cout << "g"; } if(znaki[i] == 6){ cout << "o"; } } cout << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; int DP[MAXN]; int wybor[MAXN]; int monety[] = {3, 4, 5, 6}; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; int ileJeden = 0; char x; cin >> n; for(int i = 0; i < 8*n; i++){ cin >> x; if(x == '1'){ ileJeden++; } } //cout << ileJeden << "\n"; int minimum = 0; int ind = 0; int k = 4; for(int i = 0; i <= ileJeden; i++){ wybor[i] = -1; } for(int i = 1; i <= ileJeden; i++){ minimum = ileJeden+1; for(int j = 0; j < k; j++){ if(i-monety[j] >= 0){ if(minimum > DP[i-monety[j]]+1){ minimum = DP[i-monety[j]]+1; ind = j; } } } DP[i] = minimum; wybor[i] = ind; } if(DP[ileJeden] > ileJeden || DP[ileJeden] < 0){ cout << "NIE\n"; return 0; } //odzyskiwanie vector<int> znaki; int ile = 0; int ile6 = 0; while(ileJeden > 0){ ile++; //cout << "x" << monety[wybor[ileJeden]] << "\n"; if(monety[wybor[ileJeden]] == 6){ ile6++; }else{ znaki.push_back(monety[wybor[ileJeden]]); } ileJeden = ileJeden - monety[wybor[ileJeden]]; } /*for(auto x: znaki){ cout << x << " "; } cout << "\n" << ile << "\n";*/ if(ile < n){ while(ile6 > 0 && ile < n){ ile++; ile6--; znaki.push_back(3); znaki.push_back(3); } if(ile != n){ cout << "NIE\n"; return 0; } } while(ile6 > 0){ znaki.push_back(6); ile6--; } /*for(auto x: znaki){ cout << x << " "; } cout << "\n";*/ for(int i = 0; i < znaki.size(); i++){ if(znaki[i] == 3){ cout << "a"; } if(znaki[i] == 4){ cout << "c"; } if(znaki[i] == 5){ cout << "g"; } if(znaki[i] == 6){ cout << "o"; } } cout << "\n"; } |