【topcoder】LR
#include <iostream> #include <vector> #include <numeric> #include <deque> using namespace std; class MyList { private: deque<long long> a; public: MyList(const vector<long long> &b) { a = deque<long long>(); for (auto x : b) a.push_back(x); } void execute_L() { auto tmp = a; for (int i = 1;i<a.size();i++) { tmp[i] = tmp[i] + a[i-1]; } tmp[0] += a[a.size() - 1]; a = tmp; } void execute_R() { execute_L(); rotate_L(); } long long getSum() { return accumulate(a.begin(),a.end(),0LL); } void rotate_L() { auto tmp = a[0]; a.pop_front(); a.push_back(tmp); } bool equal(const MyList &b) { return a == b.a; } // void print() { // for (auto x : a) { // cout << x << " "; // } // cout << endl; // } }; class LR { public: LR() { } string construct(vector<long long> s, vector<long long> t) { MyList list1 = MyList(s); MyList list2 = MyList(t); // list1.print(); // cout << list1.getSum() << endl; // list2.print(); // cout << list2.getSum() << endl; int stepCnt = 0; if (list1.getSum()) while (list1.getSum() < list2.getSum()) { list1.execute_L(); //list1.print(); stepCnt++; } if (list1.getSum() != list2.getSum()) return "No solution"; int rotateCnt = 0; while (!list1.equal(list2)) { list1.rotate_L(); rotateCnt++; if (rotateCnt > stepCnt) return "No solution"; } string ret = ""; for (int i = 0;i<rotateCnt;i++) ret+="R"; while (ret.size() < stepCnt) ret+="L"; return ret; } private: }; int main() { LR inst = LR(); cout << inst.construct(vector<long long>{0,0,0,0,0},vector<long long>{0,0,0,1,0}) << endl; return 0; }
思路还是想很快的,就是写个程序老纠结了。完全忍不了以前那种只为了正确的程序,这样就浪费了好多宝贵的比赛时间。而且C++也是忘得差不多了。权当娱乐了,250还是稳切的哈哈。