• Minimal string CodeForces – 797C


    题目链接

    题目难度: 1700rating

    题目类型:string+贪心+STL

    题目思路:

    由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在,就先把后面的所有的该字符放在答案字符串u中(u可以用queue来表示),而字符串t可以用stack来表示。
    中间的字符串都加入到stack中,当一个字符全加入后,继续从小到大的查找剩余字符串中还剩的字符,并且与栈顶元素进行比较,如果小于栈顶元素,需要先输出栈顶元素。
    中间的细节比较多,详细看代码。
    我用的map来维护剩余的字符数量。

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #include <stack>
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb std::ios::sync_with_stdio(false)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define gg(x) getInt(&x)
    using namespace std;
    typedef long long ll;
    inline void getInt(int* p);
    const int maxn=1000010;
    /*** TEMPLATE CODE STARTS HERE ***/
    char s[maxn];
    int n;
    map<char ,int > m;
    int main()
    {
        scanf("%s",s);
        n=strlen(s);
        rep(i,0,n)
        {
            int x=m[(s[i])];
            m[(s[i])] =x+1;
        }
        stack<char> st;
        queue<char> q;
        while(!q.empty())
        {
            q.pop();
        }
        while(!st.empty())
        {
            st.pop();
        }
        char x='a'-1;
        int index=0;
        while(q.size()!=n)
        {
            char i;
            for( i='a';i<='z';i++)
            {
                if(m.count(i)!=0&&m[i]!=0)
                {
                    x=i;
    
                    break;
                }
            }
            if(i=='z'+1)
            {
                if(!st.empty())
                    x=st.top();
            }
            if(!st.empty()&&st.top()<x)
            {
                x=st.top();
            }
    
            while(!st.empty()&&st.top()==x)
            {
                st.pop();
                q.push(x);
                if(!st.empty()&&st.top()<x)
                {
                    x=st.top();
                }
            }
            if(m.count(x)==0||m[x]==0)
                continue;
            int num=m[x];
            m.erase(x);
            for(int j=index;j<n;j++)
            {
                if(s[j]==x)
                {
                    q.push(x);
                    num--;
                }else
                {
                    st.push(s[j]);
                    m[s[j]]=m[s[j]]-1;
                }
                if(!num)
                {
                    index=j+1;
                    break;
                }
            }
    
        }
        while(!q.empty())
        {
            printf("%c",q.front());
            q.pop();
        }
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    codeforces 1257 E、F
    hdu5954
    Codeforces Round #597 (Div. 2)
    最长公共子序列
    Codeforces Problem
    Vim 编译运行gcc、g++
    【Codeforces Round #107 (Div. 2)】Win or Freeze
    【Codeforces Round #107 (Div. 2)】Quantity of Strings
    【CoreForces Round #104D】Lucky Number 2
    【CoreForces Round #104C】Lucky Conversion
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10226852.html
Copyright © 2020-2023  润新知