原文链接: CodeForces 797C
上一篇: Codeforces 762C Two strings
下一篇: Codeforces 18D
一开始,给你一个字符串s;两个空字符串t和u;
你有两种合法操作;
1.将s的开头字符加到t后面;
2.将t的最后一个字符加到u的后面去
要求最后使得s和t字符串变成空串;
并且得到的u的字符串的字典序最小
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define LL long long
int const MAX = 1e6 + 1;
int const INF = 1 << 30;
using namespace std;
char s[MAX], t[MAX], u[MAX];
int main(int argc, char* argv[]){
scanf("%s", s);
int lens = strlen(s);
//找到s中最小的字母下标
int ps = 0, pt = 0, pu = 0;
while(pu < lens){
//找a中最小字母下标
int minindex = -1;
char minca = 'z';
for(int i = ps ; i < lens ; i++){
if(s[i] == minca)
minindex = i;
else if(s[i] < minca){
minindex = i;
minca = s[i];
}
}
if(pt == 0 || t[pt - 1] > minca){
for(int i = ps ; i <= minindex ; i++){
if(s[i] == minca)
u[pu++] = minca;
else
t[pt++] = s[i];
ps++;
}
} else if(t[pt - 1] <= minca){
while(pt > 0 && t[pt - 1] <= minca){
u[pu++] = t[pt - 1];
pt--;
}
for(int i = ps ; i <= minindex ; i++){
if(s[i] == minca)
u[pu++] = minca;
else
t[pt++] = s[i];
ps++;
}
}
if(ps == lens)
for(int i = pt - 1 ; i >= 0 ; i--)
u[pu++] = t[i];
}
u[pu] = 0;
printf("%s\n", u);
return 0;
}
/*
abcabdc
aabcbcd
*/