2019 meter of garlic Quarterfinal D. "Nebula System" (monotone Stack)

VIPKID is an online children's English education platform, network stability is the quality of online education courses red line, VIPKID with the launch of the world's most stable network system of education - "Nebula system." Nebula system is currently established 55 green sea core countries worldwide covering 3535, 1616 in 5555 the city established a national center of transit nodes, have the ability to freely switch routing in one minute, to ensure that the definition of global transoceanic classroom audio and video communications, to lay a solid foundation for the smooth classroom experience.

Hub Transport node and network nodes around the world make up this "nebula system", How complex. We now consider only network nodes on one leg, each network node compared to a character, then this branch is a string.

Now you given a string and an integer kk ss, ss request the lexicographically smallest length of the subsequence kk.

The input format
of the first row of a character string composed of lowercase letters ss, a second positive integer row kk.

Output format
line a string ans, indicate the answer.

Data scale
0 <k \ leq | s | \ leq50000000 <k≤|s|≤5000000

Sample input copy
HelloWorld
. 5
sample output copy
ellld

The meaning of problems: Thinking: set string length is n, nk need to delete characters by maintaining a monotonous stack, sequentially inserted into each character string, (deleted if the current number of characters is less than the remaining characters of nk i.e.> K), and the current top of the stack is greater than the inserted characters, then delete the stack, until the number of characters reaches deleted nk, or to meet the properties of monotone stack.




See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#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 ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=5000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int k;
char st[maxn];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    
    gbtb;
    cin>>s;
    cin>>k;
    int id=0;
    int len=strlen(s);
    repd(i,0,len-1)
    {
        // ab
        while(id>0&&st[id]>s[i]&&(id+len-i)>k)
        {
            id--;
        }
        st[++id]=s[i];
    }
    repd(i,1,k)
    {
        cout<<st[i];
    }
    cout<<endl;
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    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';
        }
    }
}

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11039088.html