1255 字典序最小的子序列

题目来源:  天津大学OJ
基准时间限制:1 秒 空间限制:131072 KB 分值: 40  难度:4级算法题
 收藏
 取消关注
给出一个由a-z组成的字符串S,求他的一个子序列,满足如下条件:

1、包含字符串中所有出现过的字符各1个。
2、是所有满足条件1的串中,字典序最小的。

例如:babbdcc,出现过的字符为:abcd,而包含abcd的所有子序列中,字典序最小的为abdc。
Input
输入1行字符串S,所有字符均为小写,字符串的长度为L。(1 <= L <= 100000)。
Output
输出包含S中所有出现过的字符,每个字符各1个,并且字典序最小的S的子序列。
Input示例
babbdcc
Output示例
abdc
李陶冶  (题目提供者)
C++的运行时限为:1000 ms ,空间限制为:131072 KB  示例及语言说明请按这里

解题思路:遍历每一个字母时,以此字母为基准继续向后遍历看是否有比其跟小的字母,如果有的话就替换,若没有就不替换。

具体操作请看代码。

法一:

#include <bits/stdc++.h>
using namespace std;

int main ()
{
    string s,t="";  //s为输入字符串,t为想要的字典序最小的子序列
    cin>>s;
    int len = s.size();
    int num[27]= {0};  //用来统计每个字母的个数

    for (int i=0; i<len; i++)
        num[s[i]-'a']++;


    int f[27]= {0};
    for (int i=0; i<len; i++)
    { //因为据题意每个字母只能出现一次,所以被加入t中的字母已被标记,不需要再次加入t中。
        if(!f[s[i]-'a'])
        {
            t+=s[i];
            char ch=s[i];

//遍历时,会相应以每个字母为基准继续向后遍历看是否可以替换更小的字母,如没有找到用num[]就不能还原了,所以用next[]来作为是否可以替换的判断条件。
            int next[27];
            for (int j=0; j<26; j++)   
                next[j]=num[j];

            int j=i+1;
            next[s[i]-'a']--;
            int id=i;
            while(j!=len)
            {
                if(f[s[j]-'a']==0)
                {//判断当前字母是否比基准小和判断基准是否还有剩余,如都是是才能替换。
                    if(s[j]<ch&&next[ch-'a']>0)
                    {
                        t[t.size()-1]=s[j];
                        ch=s[j];
                        id=j;
                    }
                    next[s[j]-'a']--;
                    if(next[s[j]-'a']<1)//遍历途中有字母已经没有了,就必须跳出此次遍历。
                        break;
                }
                j++;
            }
            char cc=t[t.size()-1];
            f[t[t.size()-1]-'a']=1;
            for (int j=i; j<=id; j++)
                num[s[j]-'a']--;
            i=id;
        }
    }
    cout<<t;
    return 0;
}



法二<用stack>:

#include <cstdio>
#include <iostream>
#include <stack>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
const int N =100001;
int main()
{
    int vis[26];
    char str[N];
    stack<char> queue1;
    map<char,int> map1;
    while(~scanf("%s",str))
    {
        while(!queue1.empty())
            queue1.pop();
        map1.clear();
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            vis[str[i]-'a']=0;
            map1[str[i]]++;
        }
        for(int i=0;i<len;i++)
        {
            map1[str[i]]--;
            if(vis[str[i]-'a'])
                continue;
            while(!queue1.empty()&&queue1.top()>str[i]&&map1[queue1.top()]>0)
            {
                vis[queue1.top()-'a']=0;
                queue1.pop();
            }
            queue1.push(str[i]);
            vis[str[i]-'a']=1;
        }
        string str1="";
        while(!queue1.empty())
        {
            str1=queue1.top()+str1;
            queue1.pop();
        }
        cout<<str1<<endl;
    }
    return 0;
}


法三<用双向队列deque>:

#include <cstdio>
#include <deque>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
const int N =100001;
int main()
{
    int vis[26];
    char str[N];
    deque<char> deque1;
    map<char,int> map1;
    while(~scanf("%s",str))
    {
        deque1.clear();
        map1.clear();
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            vis[str[i]-'a']=0;
            map1[str[i]]++;
        }
        for(int i=0;i<len;i++)
        {
            map1[str[i]]--;
            if(vis[str[i]-'a'])
                continue;
            while(!deque1.empty()&&deque1.back()>str[i]&&map1[deque1.back()]>0)
            {
                vis[deque1.back()-'a']=0;
                deque1.pop_back();
            } 
            deque1.push_back(str[i]);
            vis[str[i]-'a']=1;
        }
        while(!deque1.empty())
        {
            printf("%c",deque1.front());
            deque1.pop_front();
        }
        printf("\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值