37b91b57bc 2014-07-10 kinaba: #include <iostream> 37b91b57bc 2014-07-10 kinaba: #include <sstream> 37b91b57bc 2014-07-10 kinaba: #include <iomanip> 37b91b57bc 2014-07-10 kinaba: #include <vector> 37b91b57bc 2014-07-10 kinaba: #include <string> 37b91b57bc 2014-07-10 kinaba: #include <map> 37b91b57bc 2014-07-10 kinaba: #include <set> 37b91b57bc 2014-07-10 kinaba: #include <algorithm> 37b91b57bc 2014-07-10 kinaba: #include <numeric> 37b91b57bc 2014-07-10 kinaba: #include <iterator> 37b91b57bc 2014-07-10 kinaba: #include <functional> 37b91b57bc 2014-07-10 kinaba: #include <complex> 37b91b57bc 2014-07-10 kinaba: #include <queue> 37b91b57bc 2014-07-10 kinaba: #include <stack> 37b91b57bc 2014-07-10 kinaba: #include <cmath> 37b91b57bc 2014-07-10 kinaba: #include <cassert> 37b91b57bc 2014-07-10 kinaba: #include <tuple> 37b91b57bc 2014-07-10 kinaba: using namespace std; 37b91b57bc 2014-07-10 kinaba: typedef long long LL; 37b91b57bc 2014-07-10 kinaba: typedef complex<double> CMP; 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: class SubstringReversal { public: 37b91b57bc 2014-07-10 kinaba: vector <int> solve(string S) 37b91b57bc 2014-07-10 kinaba: { 37b91b57bc 2014-07-10 kinaba: multiset<char> chars; 37b91b57bc 2014-07-10 kinaba: for(char c: S) chars.insert(c); 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: string T = S; 37b91b57bc 2014-07-10 kinaba: for(int i=0; i<S.size(); ++i) { 37b91b57bc 2014-07-10 kinaba: if(S[i] == *chars.begin()) { 37b91b57bc 2014-07-10 kinaba: chars.erase(chars.begin()); 37b91b57bc 2014-07-10 kinaba: continue; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: // here is the point. 37b91b57bc 2014-07-10 kinaba: int best_k = i; 37b91b57bc 2014-07-10 kinaba: string best = S.substr(i); 37b91b57bc 2014-07-10 kinaba: for(int k=i+1; k<S.size(); ++k) { 37b91b57bc 2014-07-10 kinaba: string T(S.begin()+i, S.begin()+k+1); 37b91b57bc 2014-07-10 kinaba: reverse(T.begin(), T.end()); 37b91b57bc 2014-07-10 kinaba: T += S.substr(k+1); 37b91b57bc 2014-07-10 kinaba: if(best > T) 37b91b57bc 2014-07-10 kinaba: best=T, best_k=k; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: vector<int> ans; 37b91b57bc 2014-07-10 kinaba: ans.push_back(i); 37b91b57bc 2014-07-10 kinaba: ans.push_back(best_k); 37b91b57bc 2014-07-10 kinaba: return ans; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: // no need to reverse 37b91b57bc 2014-07-10 kinaba: vector<int> ans; 37b91b57bc 2014-07-10 kinaba: ans.push_back(0); 37b91b57bc 2014-07-10 kinaba: ans.push_back(0); 37b91b57bc 2014-07-10 kinaba: return ans; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: }; 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: // BEGIN CUT HERE 37b91b57bc 2014-07-10 kinaba: #include <ctime> 37b91b57bc 2014-07-10 kinaba: double start_time; string timer() 37b91b57bc 2014-07-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 37b91b57bc 2014-07-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 37b91b57bc 2014-07-10 kinaba: { os << "{ "; 37b91b57bc 2014-07-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 37b91b57bc 2014-07-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 37b91b57bc 2014-07-10 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { 37b91b57bc 2014-07-10 kinaba: bool ok = (Expected == Received); 37b91b57bc 2014-07-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 37b91b57bc 2014-07-10 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 37b91b57bc 2014-07-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 37b91b57bc 2014-07-10 kinaba: #define END verify_case(_, SubstringReversal().solve(S));} 37b91b57bc 2014-07-10 kinaba: int main(){ 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: CASE(0) 37b91b57bc 2014-07-10 kinaba: string S = "abdc"; 37b91b57bc 2014-07-10 kinaba: int __[] = {2, 3 }; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(1) 37b91b57bc 2014-07-10 kinaba: string S = "aabbcc"; 37b91b57bc 2014-07-10 kinaba: int __[] = {0, 0 }; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(2) 37b91b57bc 2014-07-10 kinaba: string S = "misof"; 37b91b57bc 2014-07-10 kinaba: int __[] = {0, 4 }; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(3) 37b91b57bc 2014-07-10 kinaba: string S = "ivan"; 37b91b57bc 2014-07-10 kinaba: int __[] = {0, 2 }; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(4) 37b91b57bc 2014-07-10 kinaba: string S = "thisseemstobeaneasyproblem"; 37b91b57bc 2014-07-10 kinaba: int __[] = {0, 13 }; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: /* 37b91b57bc 2014-07-10 kinaba: CASE(5) 37b91b57bc 2014-07-10 kinaba: string S = ; 37b91b57bc 2014-07-10 kinaba: int __[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(6) 37b91b57bc 2014-07-10 kinaba: string S = ; 37b91b57bc 2014-07-10 kinaba: int __[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: */ 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: // END CUT HERE