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 <functional> 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: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: string g_input; 4fd800b3a8 2011-02-23 kinaba: string g_program; 4fd800b3a8 2011-02-23 kinaba: LL g_num_d; 4fd800b3a8 2011-02-23 kinaba: LL g_non_d; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class EndlessStringMachine { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: string getFragment(string input, string program, int s_, int min_, int max_) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL s = s_; 4fd800b3a8 2011-02-23 kinaba: LL min = min_ - 1; 4fd800b3a8 2011-02-23 kinaba: LL max = max_; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int nd = 0, id = -1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=program.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( program[i]=='$' ) 4fd800b3a8 2011-02-23 kinaba: ++nd, id=i; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( nd == 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string result; 4fd800b3a8 2011-02-23 kinaba: for(LL i=min; i<max; ++i) 4fd800b3a8 2011-02-23 kinaba: result += (i < program.size() ? program[i] : '-'); 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else if( nd == 1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // output = program[..id]^s input program[id+1..]^s 4fd800b3a8 2011-02-23 kinaba: string L = program.substr(0, id); 4fd800b3a8 2011-02-23 kinaba: string C = input; 4fd800b3a8 2011-02-23 kinaba: string R = program.substr(id+1); 4fd800b3a8 2011-02-23 kinaba: string result; 4fd800b3a8 2011-02-23 kinaba: for(LL i=min; i<max; ++i) 4fd800b3a8 2011-02-23 kinaba: if( i < L.size()*s ) 4fd800b3a8 2011-02-23 kinaba: result += L[i%L.size()]; 4fd800b3a8 2011-02-23 kinaba: else if( i < L.size()*s + C.size() ) 4fd800b3a8 2011-02-23 kinaba: result += C[i - L.size()*s]; 4fd800b3a8 2011-02-23 kinaba: else if( i < L.size()*s + C.size() + R.size()*s ) 4fd800b3a8 2011-02-23 kinaba: result += R[(i-L.size()*s-C.size()) % R.size()]; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: result += '-'; 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: g_input = input; 4fd800b3a8 2011-02-23 kinaba: g_program = program; 4fd800b3a8 2011-02-23 kinaba: g_num_d = nd; 4fd800b3a8 2011-02-23 kinaba: g_non_d = program.size() - nd; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: string result; 4fd800b3a8 2011-02-23 kinaba: for(int i=min; i<max; ++i) 4fd800b3a8 2011-02-23 kinaba: result += theChar(i, s); 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: char theChar( LL i, LL s ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( s == 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return ( i < g_input.size() ? g_input[i] : '-' ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL cur = 0; 4fd800b3a8 2011-02-23 kinaba: for(LL ip=0; ip<g_program.size(); ++ip) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( g_program[ip]=='$' ) { 4fd800b3a8 2011-02-23 kinaba: LL len = theLen(s-1); 4fd800b3a8 2011-02-23 kinaba: if( cur<=i && i<cur+len ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: i -= cur; 4fd800b3a8 2011-02-23 kinaba: LL theLevel = 0, lvLen = g_input.size(); 4fd800b3a8 2011-02-23 kinaba: while( lvLen <= i ) 4fd800b3a8 2011-02-23 kinaba: theLevel ++, lvLen = lvLen*g_num_d + g_non_d; 4fd800b3a8 2011-02-23 kinaba: return theChar(i, theLevel); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: cur += len; 4fd800b3a8 2011-02-23 kinaba: } else { 4fd800b3a8 2011-02-23 kinaba: if( cur == i ) 4fd800b3a8 2011-02-23 kinaba: return g_program[ip]; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: cur += 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return '-'; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL theLen(LL s) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( s == 0 ) 4fd800b3a8 2011-02-23 kinaba: return g_input.size(); 4fd800b3a8 2011-02-23 kinaba: if( s > 32 ) 4fd800b3a8 2011-02-23 kinaba: return 2000000000LL; 4fd800b3a8 2011-02-23 kinaba: LL pr = theLen(s-1); 4fd800b3a8 2011-02-23 kinaba: LL nx = pr*g_num_d + g_non_d; 4fd800b3a8 2011-02-23 kinaba: return min(2000000000LL, nx); 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 input = "a"; 4fd800b3a8 2011-02-23 kinaba: string program = "$meric$"; 4fd800b3a8 2011-02-23 kinaba: int s = 6; 4fd800b3a8 2011-02-23 kinaba: int min = 1; 4fd800b3a8 2011-02-23 kinaba: int max = 35; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "americamericamericamericamericameri"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EndlessStringMachine().getFragment(input, program, s, min, max)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string input = "top"; 4fd800b3a8 2011-02-23 kinaba: string program = "$coder"; 4fd800b3a8 2011-02-23 kinaba: int s = 1; 4fd800b3a8 2011-02-23 kinaba: int min = 1; 4fd800b3a8 2011-02-23 kinaba: int max = 20; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "topcoder------------"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EndlessStringMachine().getFragment(input, program, s, min, max)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string input = "abc"; 4fd800b3a8 2011-02-23 kinaba: string program = "$x$y$z$"; 4fd800b3a8 2011-02-23 kinaba: int s = 10; 4fd800b3a8 2011-02-23 kinaba: int min = 30; 4fd800b3a8 2011-02-23 kinaba: int max = 50; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "bcyabcxabcyabczabczab"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EndlessStringMachine().getFragment(input, program, s, min, max)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string input = "xy"; 4fd800b3a8 2011-02-23 kinaba: string program = "$a$bb"; 4fd800b3a8 2011-02-23 kinaba: int s = 12; 4fd800b3a8 2011-02-23 kinaba: int min = 5000; 4fd800b3a8 2011-02-23 kinaba: int max = 5099; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "xybbbbaxyaxybbaxyaxybbbbbbbbaxyaxybbaxyaxybbbbaxyaxybbaxyaxybbbbbbaxyaxybbaxyaxybbbbaxyaxybbaxyaxybb"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EndlessStringMachine().getFragment(input, program, s, min, max)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string input = "x"; 4fd800b3a8 2011-02-23 kinaba: string program = "$y$"; 4fd800b3a8 2011-02-23 kinaba: int s = 999999999; 4fd800b3a8 2011-02-23 kinaba: int min = 5000; 4fd800b3a8 2011-02-23 kinaba: int max = 5001; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "xx"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EndlessStringMachine().getFragment(input, program, s, min, max)); } 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: