Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <unordered_map>
- int main()
- {
- std::string a = "0110";
- std::string b = "1101";
- int n = a.length();
- std::queue<std::string> state;
- state.push(a);
- std::unordered_map<std::string, int> oper;
- oper[a] = 0;
- while (!state.empty())
- {
- std::string s = state.front();
- state.pop();
- int ops = oper[s] + 1;
- for (int l = 0; l < n; ++l)
- {
- for (int r = l; r < n; ++r) // reversing
- {
- s[r] = '1' - s[r] + '0';
- if (oper.find(s) == oper.end())
- {
- state.push(s);
- oper[s] = ops;
- }
- }
- for (int j = l; j < n; ++j) // reversing back
- {
- s[j] = '1' - s[j] + '0';
- }
- }
- }
- std::cout << oper[b];
- return 0;
- }
Add Comment
Please, Sign In to add comment