SRM 158 DIV2 Level One - タイヤのローテーション
タイヤのローテーションなんて考えたこと無いよ。
タイヤのローテーションが、
1 -> 3, 2 -> 4, 3 -> 2, 4 -> 1
こんな感じ。初期設定から何フェーズ目で今の設定になるかという問題。
数学っぽくいけるかと思ったけど、イマイチわからなかったので、テーブルを元にswapさせることにした。
#include <string> #include <algorithm> using namespace std; class TireRotation { public: static int getRotation(string initial, string current) { string buffer = " "; int rot[] = {2, 3, 1, 0}; for (int rotation = 1; rotation <= 4; rotation++) { if (equal(initial.begin(), initial.end(), current.begin())) return rotation; for (int i = 0; i < initial.size(); i++) buffer[rot[i]] = initial[i]; initial = buffer; } return -1; } };
なかなかバラエティに富んでるな。