4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class IncreasingLists { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: string ans; 4fd800b3a8 2011-02-23 kinaba: typedef pair<int,pair<string,string> > key; 4fd800b3a8 2011-02-23 kinaba: map<key,bool> memo; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<string> cm; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: string makeIncreasingList(string mask) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string buf(mask); 4fd800b3a8 2011-02-23 kinaba: comma(mask, 0, 1, 0, buf); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<cm.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // try_fill 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return rec(mask, 0, "", "", buf) ? ans : "impossible"; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void comma( const string& mask, int i, int prev, int cur, string& buf ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( i==mask.size() || mask[i]==',' ) 4fd800b3a8 2011-02-23 kinaba: if( prev > cur ) 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: if( i==mask.size() ) { 4fd800b3a8 2011-02-23 kinaba: cm.push_back(buf); 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( mask[i]==',' ) { 4fd800b3a8 2011-02-23 kinaba: buf[i] = ','; 4fd800b3a8 2011-02-23 kinaba: comma( mask, i+1, cur, 0, buf ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else if( mask[i]=='?' ) { 4fd800b3a8 2011-02-23 kinaba: if( prev<=cur ) { 4fd800b3a8 2011-02-23 kinaba: buf[i]=','; 4fd800b3a8 2011-02-23 kinaba: comma( mask, i+1, cur, 0, buf ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: buf[i]='?'; 4fd800b3a8 2011-02-23 kinaba: comma( mask, i+1, prev, cur+1, buf ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else { 4fd800b3a8 2011-02-23 kinaba: buf[i] = mask[i]; 4fd800b3a8 2011-02-23 kinaba: comma( mask, i+1, prev, cur+1, buf ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool rec(const string& mask, int i, const string& prev, const string& cur, string& buf) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: key k(i, make_pair(prev,cur)); 4fd800b3a8 2011-02-23 kinaba: if( memo.count(k) ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: if( i==mask.size() || mask[i]==',' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( prev.size() > cur.size() 4fd800b3a8 2011-02-23 kinaba: || prev.size() == cur.size() && prev>=cur ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( i==mask.size() ) { 4fd800b3a8 2011-02-23 kinaba: ans = buf; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( mask[i]==',' ) 4fd800b3a8 2011-02-23 kinaba: return rec(mask, i+1, cur, "", buf); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( '0' <= mask[i] && mask[i] <='9' ) 4fd800b3a8 2011-02-23 kinaba: return rec(mask, i+1, prev, cur+mask[i], buf); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // try , 4fd800b3a8 2011-02-23 kinaba: if( !(prev.size()>cur.size() || prev.size()==cur.size() && prev>=cur) ) { 4fd800b3a8 2011-02-23 kinaba: buf[i] = ','; 4fd800b3a8 2011-02-23 kinaba: if( rec(mask, i+1, cur, "", buf) ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // try 0 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<9; ++d) 4fd800b3a8 2011-02-23 kinaba: if(cur.size()>0 || d>0) { 4fd800b3a8 2011-02-23 kinaba: buf[i] = char(d+'0'); 4fd800b3a8 2011-02-23 kinaba: if( rec(mask, i+1, prev, cur+char(d+'0'), buf) ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return memo[k] = false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const string &Expected, const string &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "??"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "10"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "???"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "1,2"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "?????????,9"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "1,2,3,4,5,9"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "??????????,9"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "impossible"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "?,10,?????????????????,16,??"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "impossible"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "?2?5??7?,??"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "12,50,70,71"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "???????????????????????????????,???"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "1,10,11,100,101,102,103,104,105,106"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<7>) { 4fd800b3a8 2011-02-23 kinaba: string mask = "??????????????????????????????????????????????????"; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "1,10,100,1000,10000,100000,1000000,100000000000000"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: