intmain(){ string ss; cin >> ss; int n; cin >> n; while (n--) { int a, b; cin >> a >> b; int n = b - a + 1; string cx = ss.substr(a - 1, n); ss.erase(a - 1, n); string s, t; cin >> s >> t; string u = s + t; int p = ss.find(u); if (p == -1) ss += cx; else ss.insert(p + s.size(), cx); } cout << ss; }
voidswap(int *a, int *b) { int t = *a; *a = *b; *b = t; } intmain() { int a = 1, b = 2; swap(&a, &b); // int t = a; // a = b; // b = t; printf("%d %d", a, b); }
1 2 3
int a = 1, b = 2; swap(a, b); cout << a << ' ' << b;
1 2 3 4 5
string a, b; a = "12"; b = "21"; swap(a, b); cout << a << '\n' << b << '\n';
reverse
逆转范围中的元素顺序
1 2 3 4 5 6 7 8 9 10 11 12 13
int a[5] = {1, 2, 3, 4, 5}; reverse(a, a + 5); vector<int> b = {1, 2, 3, 4, 5}; reverse(b.begin(), b.end()); // [ ) // [0 1 2 3 4 5) for (int i = 0; i < 5; i++) cout << a[i] << ' '; cout << '\n';
for (int i = 0; i < b.size(); i++) cout << b[i] << ' '; cout << '\n';
permutation
next_permutation
产生某个元素范围的按字典顺序的下一个较大的排列
1 2 3 4 5 6 7
vector<int> a = {1, 2, 3, 4}; do { for (int x : a) cout << x << ' '; cout << '\n'; } while (next_permutation(a.begin(), a.end())); // 4 * 3 * 2 = 24
prev_permutation
1 2 3 4 5 6 7 8
vector<int> a = {4, 3, 2, 1}; do { for (int x : a) cout << x << ' '; cout << '\n'; } while (prev_permutation(a.begin(), a.end())); // 4 * 3 * 2 = 24 // 字典序
sort
sort
将范围按升序/降序排序
1 2 3 4 5
vector<int> a = {3, 1, 2, 4, 5}; // sort(a.begin(), a.end(), less()); // < sort(a.begin(), a.end(), greater()); // > for (int x : a) cout << x << " ";
自定义排序比较器
1 2 3 4 5 6 7 8 9 10 11
boolcmp(int a, int b){ return a > b; } // <=> greater()
intmain(){ vector<int> a = {3, 1, 2, 4, 5}; sort(a.begin(), a.end(), cmp); for (int x : a) { cout << x << ' '; } }