字典序最小的子序列
来源:互联网 发布:局域网打印机端口 编辑:程序博客网 时间:2024/05/16 12:12
1、包含字符串中所有出现过的字符各1个。
2、是所有满足条件1的串中,字典序最小的。
例如:babbdcc,出现过的字符为:abcd,而包含abcd的所有子序列中,字典序最小的为abdc。
babbdcc
abdc
利用双向队列:
#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; //printf("c==%c ",deque1.back()); deque1.pop_back(); } // printf("%c ",str[i]); deque1.push_back(str[i]); vis[str[i]-'a']=1; } while(!deque1.empty()) { printf("%c",deque1.front()); deque1.pop_front(); } printf("\n"); } return 0;}利用栈:
#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; //printf("c==%c ",deque1.back()); queue1.pop(); } // printf("%c ",str[i]); 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;}
阅读全文
0 0
- 1255 字典序最小的子序列
- 字典序最小的子序列
- 字典序最小的子序列
- 51nod 1255 字典序最小的子序列
- 51nod-1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列
- 【51Nod 1255】字典序最小的子序列
- 51nod 1255 字典序最小的子序列【贪心】
- 51nod 1255 字典序最小的子序列 []【贪心】
- 1255 字典序最小的子序列 (水题+技巧)
- 51NOD 1255 字典序最小的子序列 【贪心】
- 1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列(贪心)
- 51 nod 1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列(贪心)
- 51 nod 1255 字典序最小的子序列
- 51nod 1255 字典序最小的子序列 贪心
- [hihocoder taige 20-3]最小字典序子序列
- CentOS 常用命令大全
- Apache与Tomcat 区别联系
- HelloChart折线图的改写
- 设计模式(16)-迭代器模式
- ubuntu 16.0.4安装Opencv3.2.0
- 字典序最小的子序列
- GitLab-9.3.6安装及汉化
- jquery radio设置checked但按钮radio仍然没选中的js处理方式
- Java语言基础——关键字,标识符,常量和变量
- cad转pdf格式简易步骤
- 冒泡
- Python正则match()和search()的理解
- 39个必知必会的SQL 性能调优方法
- Greatest Naruto Army