Description:
有一個字符串,現在對其進行多次局部排序,例如str=“abcdef”,輸入a=0, s=1,e=3,表示對abc這個子字符串進行降序排列:cbadef。若a=1,表示按照升序排列,a=0表示降序;s,e表示起始和終止字符的位置。
輸入例程:
10 3
naitdocexv
1 1 3
0 9 10
1 7 9
10 代表字符串的長度,3表示進行3次操作,下一行是字符串,之後是三種操作,順序是a, s, e。
知識點:
- 注意sort函數輸入的參數的範圍。左閉右開
- 注意sort默認是升序,如果要用降序,頭文件:
#include <functional
>,比較函數要用greater<類型>()
代碼:
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
string str;
string stra;
int len,n;
int a, s, e;
cin >> len >> n;
cin >> str;
while (n--)
{
cin >> a >> s >> e;
if (a == 1) //升序
{
sort(&str[s - 1], &str[e]);
}
else if (a == 0) // 降序
{
sort(&str[s - 1], &str[e],greater<char>());
}
}
cout << str << endl;
return 0;
}