• CodeForces 797C Minimal string


    栈。

    先处理一下后缀最小值。

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

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

    #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("
    ");
    	return 0;
    }
    
  • 相关阅读:
    luogu P3834 【模板】可持久化线段树 1(主席树) 查询区间 [l, r] 内的第 k 小/大值
    覆盖的面积 HDU
    Picture POJ
    Atlantis HDU
    Transformation HDU
    Tunnel Warfare HDU
    Agri-Net POJ
    Conscription POJ
    Brush (IV) LightOJ
    Throwing Dice LightOJ
  • 原文地址:https://www.cnblogs.com/zufezzt/p/6829143.html
Copyright © 2020-2023  润新知