1255 字典序最小的子序列
来源:互联网 发布:编程电缆 质量 编辑:程序博客网 时间:2024/06/06 04:01
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
- 1255 字典序最小的子序列
- 1255 字典序最小的子序列
- 字典序最小的子序列
- 字典序最小的子序列
- 51nod 1255 字典序最小的子序列
- 51nod-1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列
- 【51Nod 1255】字典序最小的子序列
- 51nod 1255 字典序最小的子序列【贪心】
- 51nod 1255 字典序最小的子序列 []【贪心】
- 1255 字典序最小的子序列 (水题+技巧)
- 51NOD 1255 字典序最小的子序列 【贪心】
- 51nod 1255 字典序最小的子序列(贪心)
- 51 nod 1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列(贪心)
- 51 nod 1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列 贪心
- 51nod 1255 字典序最小的子序列 神奇的栈用法
- php 支持断点续传的文件下载类
- Android新特性介绍,ConstraintLayout完全解析
- Java编译后产生class文件的命名规则
- C++Primer拾遗
- 一道让你轻松搞定Java面向对象方法
- 1255 字典序最小的子序列
- apache shiro使用nginx集群如何解决cache共享
- CSDN日报20170721——《为什么我们创业失败了和选择创业公司的思考》
- JavsScript学习
- 数组
- redis在window和linux下的安装
- 数组基本方法
- Tautology
- 建造者模式