zoukankan      html  css  js  c++  java
  • 51Nod1255 字典序最小的子序列

    Problem

    给出一个由a-z组成的字符串S,求他的一个子序列,满足如下条件:

    1、包含字符串中所有出现过的字符各1个。

    2、是所有满足条件1的串中,字典序最小的。

    例如:babbdcc,出现过的字符为:abcd,而包含abcd的所有子序列中,字典序最小的为abdc。

    Solution

    一个栈,当前没有在栈内,栈顶不是最后一个,且当前更小,就出1个入1个。

    Code

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<vector>
    #include<cstring>
    #include<stack>
    
    #define mem(ss) memset(ss,0,sizeof(ss))
    #define rep(d, s, t) for(int d=s;d<=t;d++)
    #define rev(d, s, t) for(int d=s;d>=t;d--)
    typedef long long ll;
    typedef long double ld;
    typedef double db;
    const ll mod = 998244353;
    const int N = 1e4 + 10;
    #define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    using namespace std;
    
    ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
    
    inline ll read() {
        ll data = 0;
        char ch = 0;
        while (ch < '0' || ch > '9') ch = getchar();
        while (ch >= '0' && ch <= '9') data = data * 10 + ch - '0', ch = getchar();
        return data;
    }
    stack<char>s;
    int len;
    char x[100020];
    bool f[30];
    int las[30];
    char ans[30];
    int main() {
        scanf("%s",x);
        len=strlen(x);
        for(int i=0;i<len;i++){
            las[x[i]-'a']=i;
        }
        for(int i=0;i<len;i++){
            while(!s.empty()&&x[i]<s.top()&&las[s.top()-'a']>i&&!f[x[i]-'a']){
                f[s.top()-'a']=false;
                s.pop();
    
            }
            if(!f[x[i]-'a']){
                s.push(x[i]);
                f[x[i]-'a']=true;
            }
        }
        int t=0;
        while(!s.empty()){
            ans[++t]=s.top();
            s.pop();
        }
        for(int i=t;i>=1;i--){
            printf("%c",ans[i]);
        }
        return 0;
    }
    
  • 相关阅读:
    LeetCode 931. Minimum Falling Path Sum
    LeetCode 1. Two Sum
    LeetCode 72. Edit Distance
    命令行发送SMTP协议邮件(163邮箱)
    商品销量分析及预测系统测试心得
    商品销量预测 第一次迭代总结
    【Codeforces 126B】Password
    【Codeforces 466C】Number of Ways
    【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap
    【Codeforces Global Round 1 E】Magic Stones
  • 原文地址:https://www.cnblogs.com/sz-wcc/p/11664015.html
Copyright © 2011-2022 走看看