Minimal string CodeForces - 797C

46 篇文章 0 订阅
17 篇文章 0 订阅

Petya recieved a gift of a string s with length up to 10^5 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves:
1.Extract the first character of s and append t with this character.
2.Extract the last character of t and append u with this character.
Petya wants to get strings s and t empty and string u lexicographically minimal.
You should write a program that will help Petya win the game.

Input
First line contains non-empty string s (1 ≤ |s| ≤ 10^5), consisting of lowercase English letters.

Output
Print resulting string u.

Example
Input
cab
Output
abc
Input
acdb
Output
abdc

大致题意:给你一个字符串s(由小写字母组成)和两个空串t,u,现在有两个操作,1.将字符串s的第一个字符移动添加到t串的末尾。2.将t串末尾的字符移动添加到u串的末尾。让你输出u串填满后的最小字典序。

思路:简单的模拟,贪心的去找当前s串和t串末尾中最小的那个字符,然后将其移动添加到u串里输出。

代码如下

#include <cstdio>  
#include <cstring>  
#include <iostream> 
#include <algorithm>   
#define ll long long

using namespace std; 
char s[100005];//s串
char s2[100005];//t串
int num[26];
int main()
{
    std::ios::sync_with_stdio(false);
    memset(num,0,sizeof(num));
    cin>>s;
    int l=strlen(s);
    for(int i=0;i<l;i++)//记录每种字母出现的次数
    num[s[i]-'a']++;

    int len=0;//s2串长度
    int x;
    char c;
    for(int i=0;i<26;i++)//找出当前s串中最小的字符
    {
        if(num[i]!=0)
        {
            c=i+'a';
            break;
        }
    }

    int i=0;
    for(i=0;i<l;i++)//s串中找到第一个最小字符c输出,前面的保存到s2串中
    {
        if(s[i]!=c)
        {
            s2[len]=s[i];
            len++;
            num[s[i]-'a']--;
        }
        else
        {
            cout<<c;
            num[c-'a']--;
            break;
        }
    }

    i++;
    while(i<l)
    {
        int flag=0;
        if(len==0)//如果s2串长度为0即s2串中此时没有字符
        {
            for(int i=0;i<26;i++)//找到此时的s串中最小字符
            {
                if(num[i]!=0)
                {
                c=i+'a';
                break;
                }
            }
            flag=1;
        }
        else 
        {

            for(int j=0;j<s2[len-1]-'a';j++)//否则判断此时s串中是否有最小字符小于s2串的末尾字符
            if(num[j]!=0)
            {
            c='a'+j;
            flag=1;
            break;
            }
        }

        if(flag==1)//选择此时s串中的最小字符输出,在这前面的接着存到s2串里
        {
            while(1)
            {
                if(s[i]!=c)
                {

                    s2[len]=s[i];
                    len++;
                    num[s[i]-'a']--;
                    i++;
                }
                else
                {
                    cout<<c;
                    num[c-'a']--;
                    i++;
                    break;
                }
            }
        }
        else //选择s2串的最后一个字符
        {
            cout<<s2[len-1];
            len--;
        }
    }
    for(int j=len-1;j>=0;j--)//最后倒着输出s2串中的字符
    cout<<s2[j];
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值