(找最小的包含每个字符的字符串) CSL 的字符串

链接:https://ac.nowcoder.com/acm/contest/551/D
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

CSL 以前不会字符串算法,经过一年的训练,他还是不会……于是他打算向你求助。
 

给定一个字符串,只含有可打印字符,通过删除若干字符得到新字符串,新字符串必须满足两个条件:

  • 原字符串中出现的字符,新字符串也必须包含。
  • 新字符串中所有的字符均不相同。
  • 新字符串的字典序是满足上面两个条件的最小的字符串。

输入描述:

仅一行,有一个只含有可打印字符的字符串 s。

|s|≤105|s|≤105

输出描述:

在一行输出字典序最小的新字符串。

示例1

输入

复制

bab

输出

复制

ab

示例2

输入

复制

baca

输出

复制

bac

备注:

ASCII字符集包含 94 个可打印字符(0x21 - 0x7E),不包含空格。
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
char s[N];
set<char>ss;         // 判断每个字符是否出现过
map<char,int>mapp;   // 每个字符出现的次数
vector<char>v;       // 保留最终结果
int main(){
    cin.getline(s,N);
    int len=strlen(s);
    for(int i=0;i<len;i++)
        mapp[s[i]]++;

    for(int i=0;i<len;i++){
        if(!ss.count(s[i])){
           // printf("c === %c\n",s[i]);
           // 如果该字符比原串最后一个字符小,就不断更新(且要保障删除掉的字符在此之后还存在)
            while(v.size()&&mapp[v.back()]>0&&v.back()>s[i]){
                ss.erase(v.back());
                v.pop_back();
            }
            ss.insert(s[i]);
            v.push_back(s[i]);
        }
        //  每移动一个下标,当前值的数量减少
        mapp[s[i]]--;
    }
    for(int i=0;i<v.size();i++)
        printf("%c",v[i]);
    return 0;
}

//   !@$E%RT&Y*$%$#$$#$#$#$~~@@!~!#

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/88933302