CodeForces - 797C Minimal string (贪心)

2024-02-02 08:48

本文主要是介绍CodeForces - 797C Minimal string (贪心),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意:给出string s长度<=1e5, op1:把s的第一个字符移动到t末尾.op2:把t最后一个字符移到u末尾,求u能得到的最小字典序?
思路:
用逆序维护一个数组Min,Min是用来表示其元素后边最小的字符,用Min是用来跟栈顶元素比较,如果栈顶元素小于等于的话就输出栈顶元素,继续用栈顶元素跟Min比较,直到不符合条件为止 。
同时每个字符都在操作结束后入栈。

#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXN = 1e5+7;
char s[MAXN];
int Min[MAXN];
int main()
{while(~scanf("%s",&s)){int n=strlen(s);memset(Min,0,sizeof(Min));for(int i=n-1;i>=0;i--){int a=s[i];if(i==n-1) Min[i]=a;else Min[i]=min(Min[i+1],a);//min只能是两个类型比较 }stack<char>sta;for(int i=0;i<n;i++){if(sta.empty())sta.push(s[i]);else{while(!sta.empty()) //这个栈的判断必须写,因为不写就会出现栈顶元素底下的元素比要加入的元素小却没被输出的情况 {char c=sta.top();if(c<=Min[i]){printf("%c",c);sta.pop();}elsebreak;}sta.push(s[i]);}}while(!sta.empty()){char c=sta.top();printf("%c",c);sta.pop();}printf("\n");}
} 

这篇关于CodeForces - 797C Minimal string (贪心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/670147

相关文章

【服务器运维】CentOS6 minimal 离线安装MySQL5.7

1.准备安装包(版本因人而异,所以下面的命令中版本省略,实际操作中用Tab自动补全就好了) cloog-ppl-0.15.7-1.2.el6.x86_64.rpmcpp-4.4.7-23.el6.x86_64.rpmgcc-4.4.7-23.el6.x86_64.rpmgcc-c++-4.4.7-23.el6.x86_64.rpmglibc-2.12-1.212.el6.x86_64.r

【服务器运维】CentOS7 minimal 离线安装 gcc perl vmware-tools

0. 本机在有网的情况下,下载CentOS镜像 https://www.centos.org/download/ 1. 取出rpm 有的情况可能不需要net-tools,但是如果出现跟ifconfig相关的错误,就把它安装上。另外如果不想升级内核版本的话,就找对应内核版本的rpm版本安装 perl-Time-Local-1.2300-2.el7.noarch.rpmperl-Tim

1_CString char* string之间的关系

CString转char*,string string转char*,CString char* 转CString,string 一、CString转char*,string //字串转换测试 CString CString1; std::string string1; CHAR* char1=NULL; //1string1=CString1.GetBuffer();CStri

query string parameters 和request payload

HTTP请求中,如果是get请求,那么表单参数以name=value&name1=value1的形式附到url的后; post请求:表单参数是在请求体中,也是name=value&name1=value1的形式在请求。 export const voucherDetailAdd=(token,formStr) =>{return axios.post(`${base}/voucher/deta

【Java】ArrayListString转化为String数组问题

Java的容器类Collections中toArray()方法,可以把诸如ArrayList<String>的动态数组、不定长转化静态数组、定长数组String[] 但是,如下的转化方式是错误的。 [java]  view plain copy String[] strArray = (String[]) arrayList.toArray();   如果这样执行会导致

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

C++:字符串string类使用

C++字符串和C字符串的对比 (1)C语言严格说没有字符串的概念,C字符串其实就是字符数组或字符指针 (2)C++和之后的java等都有字符串,本质是一个class (3)C++字符串的优势是标准库自带可用于字符串的各种处理算法和方法 (4)C++实际开发中建议使用C++字符串而不是沿用C式字符串 字符串string类使用 std::string str = "Hello, Worl

Java中String和StringBuffer的区别?

String 不是简单类型,而是一个类,它被用来表示字符序列。字符本身符合 Unicode 标准,其初始化方式有两种。如:String greeting=“Good Morning! \n”;String greeting=new String(=“Good Morning! \n”);String的特点是一旦赋值,便不能更改其指向的字符对象,如果更改,则会指向一个新的字符对象 。StringB