CF1251C Minimize The Integer 题解 双指针

题目链接:https://www.luogu.com.cn/problem/CF1251C

解题思路:双指针。
感觉这道题是一道讲解 双指针 的非常好的题目,题解有时间补上。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 300030;
int T, n;
char s[maxn];
bool vis[maxn];
int main() {
    cin >> T;
    while (T --) {
        cin >> s;
        for (n = 0; s[n]; n ++) vis[n] = false;
        int j = 0;
        for (int i = 0; i < n; i ++) {
            if (vis[i]) continue;
            while (j <= i) j ++;
            while (j<n && ( vis[j] || s[i]%2==s[j]%2 || s[j]<s[i] )) {
                if (s[i]%2 != s[j]%2 && s[j] < s[i]) {
                    vis[j] = true;
                    cout << s[j];
                }
                j ++;
            }
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/quanjun/p/12740213.html