Problem Statement |
|
You are playing a solitaire game called left-right digits game. this game uses a deck of N cards. each card has a single digit written on it. these digits are given as characters in the stringDigits. More precisely, the I-th character OfDigitsIs the digit written on the I-th card from the top of the deck (both indices are 0-based ).The game is played as follows. First, you place the topmost card (whose digit is the 0-th characterDigits) On the table. Then, you pick the cards one-by-one from the top of the deck. For each card, you have to place it either to the left Or to the right of all cards that are already on the table. After all of the cards have been placed on the table, they now form an N-digit number. You are given a stringLowerboundThat represents an N-digit number. The primary goal of the game is to arrange the cards in such a way that the number x Shown on the cards will be greater than or equalLowerbound. If there are multiple ways to satisfy the primary goal, you want to make the number X as small as possible. Return the smallest possible value of X you can achieve, as a string containing N digits. If it is impossible to achieve a number which is greater than or equalLowerbound, Return an empty string instead. |
Definition |
|
Class: |
Leftrightdigitsgame2 |
Method: |
Minnumber |
Parameters: |
String, string |
Returns: |
String |
Method signature: |
String minnumber (string digits, string lowerbound) |
(Be sure your method is public) |
|
|
|
Notes |
- |
LowerboundHas no leading zeros. This means that any valid number x shocould also have no leading zeros (since otherwise it will be smallerLowerbound). |
Constraints |
- |
DigitsWill contain between 1 and 50 characters, inclusive. |
- |
Each characterDigitsWill be between '0' and '9', inclusive. |
- |
LowerboundWill contain the same number of charactersDigits. |
- |
Each characterLowerboundWill be between '0' and '9', inclusive. |
- |
The first characterLowerboundWill not be '0 '. |
Examples |
0) |
|
|
|
Returns: "556" |
You can achieve exactly 556. The solution is as follows:
- Place the first card on the table.
- Place the second card to the right of all cards on the table.
- Place the last card to the left of all cards on the table.
|
|
|
1) |
|
|
|
2) |
|
|
|
Returns: "" |
The largest number you can achieve is 655, but it is still less than 656. |
|
|
3) |
|
|
"9876543210" |
"5565565565" |
|
Returns: "5678943210" |
|
|
4) |
|
|
|
Question: Here are two strings. You need to use the first string to extract from left to right. Each time, you can place it on the left or right of the new string so that the new string is greater than or equal to the second string, and minimize this string.
Analysis: there was no idea at the beginning, but it was probably a bit of thought to turn the process back. select several characters from the right to the left and put them at the beginning. The remaining character order is followed by the following, that is to say, when such a string meets the conditions, my first response is DFS... Then there will be endless wa... Finally, we got a set of data, tle... Tle
I guess the positive solution is DP, so I read the answer below. It's a very interesting status representation. I will not elaborate on it. I can do it now.
Code:
// BEGIN CUT HERE// END CUT HERE#line 5 "LeftRightDigitsGame2.cpp"#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<sstream>#include<map>#include<set>#include<queue>#include<stack>#include<fstream>#include<numeric>#include<iomanip>#include<bitset>#include<list>#include<stdexcept>#include<functional>#include<utility>#include<ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(i=0;i<(n);++i)#define FOR(i,l,h) for(i=(l);i<=(h);++i)#define FORD(i,h,l) for(i=(h);i>=(l);--i)typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;class LeftRightDigitsGame2{public: bool vis[55][55][3][3]; string f[55][55][3][3],ds,lb,ans; int n; string solve(int l,int r,int sl,int sr) { if(l+r>=n) { if(sl==0||(sl==1&&sr==0))return "*"; return ""; } string &ret=f[l][r][sl][sr],tmp; if(vis[l][r][sl][sr])return ret; vis[l][r][sl][sr]=1; ret="*"; int slt=sl,srt=sr; if(sl==1) { if(ds[n-l-r-1]<lb[l])slt=0; if(ds[n-l-r-1]>lb[l])slt=2; } tmp=solve(l+1,r,slt,sr); if(tmp!="*")ret=ds[n-l-r-1]+tmp; if(ds[n-l-r-1]>lb[n-r-1])srt=2; if(ds[n-l-r-1]<lb[n-r-1])srt=0; tmp=solve(l,r+1,sl,srt); if(tmp!="*") { tmp=tmp+ds[n-l-r-1]; if(ret=="*")ret=tmp; else ret=min(ret,tmp); } return ret; } string minNumber(string _ds, string _lb) { ds=_ds,lb=_lb; memset(vis,0,sizeof(vis)); n=ds.size(); ans=solve(0,0,1,1); if(ans=="*")ans=""; return ans; }// BEGIN CUT HEREpublic:void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }private:template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }void test_case_0() { string Arg0 = "068"; string Arg1 = "356"; string Arg2 = "608"; verify_case(0, Arg2, minNumber(Arg0, Arg1)); }void test_case_1() { string Arg0 = "310938"; string Arg1 = "847579"; string Arg2 = "890133"; verify_case(1, Arg2, minNumber(Arg0, Arg1)); }void test_case_2() { string Arg0 = "565"; string Arg1 = "656"; string Arg2 = ""; verify_case(2, Arg2, minNumber(Arg0, Arg1)); }void test_case_3() { string Arg0 = "9876543210"; string Arg1 = "5565565565"; string Arg2 = "5678943210"; verify_case(3, Arg2, minNumber(Arg0, Arg1)); }void test_case_4() { string Arg0 = "8016352"; string Arg1 = "1000000"; string Arg2 = "1086352"; verify_case(4, Arg2, minNumber(Arg0, Arg1)); }// END CUT HERE};// BEGIN CUT HEREint main(){ LeftRightDigitsGame2 ___test; ___test.run_test(-1); return 0;}// END CUT HERE