首页 > 其他 > 详细

CodeForces 797C Minimal string

时间:2017-05-09 10:48:33      阅读:262      评论:0      收藏:0      [点我收藏+]

栈。

先处理一下后缀最小值。

对于每一个字符,如果不是后缀最小值,将栈顶小于当前后缀最小值的都弹出,然后压入当前字符。

如果是后缀最小值,将栈顶小于当前后缀最小值的都弹出,再输出该字符。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

char s[100010];
char m[100010];
stack<char>st;

int main()
{
	scanf("%s",s);
	
	int len = strlen(s);
	m[len-1] = s[len-1];
	for(int i=len-2;i>=0;i--)
	{
		m[i] = min(m[i+1],s[i]);
	}

	for(int i=0;i<len;i++)
	{
		if(s[i] == m[i]) 
		{
			while((!st.empty())&&(st.top()<=s[i]))
			{
				printf("%c",st.top());
				st.pop();
			}
			printf("%c",s[i]);
		}
		else 
		{
			while((!st.empty())&&(st.top()<=m[i]))
			{
				printf("%c",st.top());
				st.pop();
			}
			st.push(s[i]);
		}
	}

	while(!st.empty())
	{
		printf("%c",st.top());
		st.pop();
	}
	printf("\n");
	return 0;
}

 

CodeForces 797C Minimal string

原文:http://www.cnblogs.com/zufezzt/p/6829143.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!