这道题类似于《编程之法》中的字符串旋转,也可以用双指针解决。还有,注意reverse(start, end)的用法。
时间复杂度:O(n^2)
空间复杂度:O(n)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void reverseString() {
string str, a, b, res = "false";
cin >> str;
a = str.substr(0, str.find(';'));
b = str.substr(str.find(';') + 1);
string t = a;
for (int i = 0; i <= a.size(); i++) {
reverse(t.begin(), t.begin() + i);
reverse(t.begin() + i, t.end());
reverse(t.begin(), t.end());
if (t == b) {
res = "true";
break;
}
t = a;
}
cout << res << endl;
}
int main()
{
reverseString();
return 0;
} 


京公网安备 11010502036488号