题目描述:
给定一个字符串,要求将字符串前面的若干个字符移到字符串的尾部。例如,将字符串"abcdef"的前3个字符'a','b'和'c'移到字符串的尾部,那么原字符串将变成"defabc"。
新建空间法:
新建一个新的字符串,长度与原字符串长度相同为n,设移动字符数为x。将原字符串的0~(x-1)位的字符移动到新字符串的(n-x+1)~n位。原字符串中的x~n位的字符移动到新字符串的0~(n-x)位。共需要n次移动操作,时间复杂度O(n),空间复杂度O(n)。
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter the string: ";
string str;
getline(cin, str);
cout << "Enter the number: ";
int n;
cin >> n;
int len = str.length();
string strnew(len,' '); //make a new string with space
int i;
for (i = 0; i < len - n; i++)
strnew[i] &